Why is the output: match 1. match 2 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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")

27th Jul 2018, 3:02 PM
ADITYA SINHA
ADITYA SINHA - avatar
1 Answer
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.
27th Jul 2018, 3:35 PM
Alexander
Alexander - avatar