Regular Expressions - Using Special Sequences
Hello, I understand and know how this code works: >>> 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") <<< It's the first example for the lesson "special sequences" in the module "regular expressions" (Course: Python Core) and outputs Match 1 and Match 2. But what I don't understand is why changing the regex-pattern to r"(.)+ \1" outputs Match 3 and the regex-pattern r"(.) \1" doesn't output anything at all. I would have naively expected that both changes shouldn't change the output at all. But it obviously does. More extremely, it evokes quite the opposite output. Could anyone explain why Python behaves like this? Thanks in advance and cheers to all helping coders out there!