我試圖通過pattern.findall
匹配字符串中的所有模式,但它僅適用部分re.Pattern.findall工作錯誤
代碼
#--coding:UTF-8 --
import re
import pprint
regex = r"(19|20|21)\d{2}"
text = "1912 2013 2134"
def main():
pattern = re.compile(regex)
print pattern.findall(text)
if __name__ == '__main__':
main()
,並打印:
['19', '20', '21']
應該打印['1912', '2013','2134']
thx!但我必須問爲什麼它的作品。 – ssj
@我已經將非捕獲組進行了精美的解釋。請檢查一次。 – thefourtheye
我已經知道了你的想法,如果'findall'用於匹配組,它只會匹配組中的所有內容。所以如果我嘗試'((19 | 20 | 21)\ d {2})',結果將會是'[('1912','19'),('2013','20'),('2134 ','21')]' – ssj