Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想匹配 12.12a 或 13.12b 之类的东西,但下面的正则表达式与 'a' 匹配,我不知道为什么会这样
import re pattern = re.compile('\d\d?\.\d\d?(a|b)') txt = "12.12a" pattern_list = re.findall(pattern,txt) for item in pattern_list: print(item) # result a
将表达式放入括号中。当有括号时,仅matching groups匹配并返回括号中的人员(要精确)
matching groups
pattern = re.compile('(\d\d?\.\d\d?(a|b))')
结果是 ('12.12b', 'a') 因为内部括号。要摆脱内部括号匹配,请使用 item[0] 或其他适当的操作。或者只是展开正则表达式(可能会慢一点)
pattern = re.compile('\d\d?\.\d\d?a|\d\d?\.\d\d?b')