Regex for extracting the substring of a given type from a string

Can someone help me in finding the regular expression for extracting the substring "CT23123" from the string "Andy Joe:CT23123" . I need a regex for extracting anything that is after ':' and is followed by two alphabets (can be in caps or small case) and 5 digits.


尝试这个:

/:([a-zA-Z]{2}d{5})/


r = /
    (?<=:)    # match a colon in a positive lookbehind
    [A-Z]{2}  # match two letters
    d{5}     # match 5 digits
    (?=D|z) # match a non-digit or the end of the string in a positive lookahead
    /xi       # extended/free-spacing mode (x) and case-indifferent (i)

"Andy Joe :CT23123"[r] #=> "CT23123" 
"Andy Joe:CT23123a"[r] #=> "CT23123" 
"Andy Joe:CT231234"[r] #=> nil 

要么:

r = /
    :               # match a colon
    ([A-Z]{2}d{5}) # match two letters followed by 5 digits in capture group 1
    (?:D|z)       # match a non-digit or the end of the string in a non-capture group
    /xi             # extended/free-spacing mode (x) and case-indifferent (i)

"Andy Joe :CT23123"[r,1] #=> "CT23123" 
"Andy Joe:CT23123a"[r,1] #=> "CT23123" 
"Andy Joe:CT231234"[r,1] #=> nil 

使用不区分大小写选项的另一个版本:

/:([a-z]{2}d{5})/i
链接地址: http://www.djcxy.com/p/87020.html

上一篇: Java正则表达式:从多次出现的模式中提取子字符串

下一篇: 用于从字符串中提取给定类型的子字符串的正则表达式