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

有人可以帮我找到从字符串“Andy Joe:CT23123”中提取子串“CT23123”的正则表达式。 我需要一个正则表达式来提取':'后面的任何内容,后面跟着两个字母(可以是大写或小写)和5位数字。


尝试这个:

/:([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/87019.html

上一篇: Regex for extracting the substring of a given type from a string

下一篇: Java extract substring from url string