+ 1
Why is the output: match 1. match 2
import re pattern = r"(.+) \1" match = re.match(pattern, "word word") if match: print ("Match 1") match = re.match(pattern, "?! ?!") if match: print ("Match 2") match = re.match(pattern, "abc cde") if match: print ("Match 3")
1 Antwort
0
\1 means the thing that appeared in the first parentheses. The first parentheses included .+, which matches anything. So the regular expression is essentially something, space, that something again. “word word” and “?! ?!” both match that pattern.