[英]Python Regular Expression for String Matching
我可以使用哪个正则表达式来匹配模式字符串 - XX-YYY-XXXXXZ,其中 X 是任何数字,YYY 是需要匹配的任何字母模式,Z 是字母表。
例如。 如果三个字符串是 89-ABC-98765Z、76-GHI-67453H、76-ABC-76453A 我需要带有“ABC”的 output 字符串,即 89-ABC-98765Z、76-ABC-76453A
尝试使用 str.match(r'.[0-9][ABC][0-9][AZ]?')。 PS 我正在尝试在 dataframe 列中使用它。
import re
pattern = r'\d+-[A-Z]+-\d+[A-Z]'
text = 'for eg. if three strings are 89-ABC-98765Z, 76-GHI-67453H, 76-ABC-76453A I need the output strings with "ABC" i.e 89-ABC-98765Z, 76-ABC-76453A'
res = re.findall(pattern,text)
print(res)
正则表达式应该是:
\d{2}-ABC-\d{5}[A-Z]
解释:
\d{2} # 2 digits
-ABC- # literal "-ABC-"
\d{5} # 5 digits
[A-Z] # any uppercase letter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.