有人可以帮助我找到用于提取子串的正则表达式" CT23123"来自字符串" Andy Joe:CT23123" 。我需要一个正则表达式来提取在'之后的任何内容:'然后是两个字母(可以是大写或小写)和5位数字。
答案 0 :(得分:1)
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
答案 1 :(得分:0)
试试这个:
Security
答案 2 :(得分:0)
使用不区分大小写的选项的另一个版本:
/:([a-z]{2}\d{5})/i
答案 3 :(得分:0)