- 1

about pattern pattern = r"(.+) \1" and pattern = r"(.+) \2"

Hi Dear, Can you please explain these pattern for me? pattern = r"(.+) \1" and pattern = r"(.+) \2" when i use the following script, there is no problem: import re pattern = r"(.+) \1" match = re.match(pattern, "word word") if match: print ("Match 1") but when i change the pattern to r"(.+) \2" it rise an error. please explain exactly what is this pattern mean. import re pattern = r"(.+) \2" match = re.match(pattern, "egg egg egg") if match: print ("Match 1")

15th May 2017, 10:50 AM
Farrokh Yousefi
1 Odpowiedź
+ 4
In Regular expression, the '\1' references the first capturing group. And the '\2' referencees the second capturing group. Forexample, pattern = r"(.+) (.+) \1 \2" match = re.match(pattern, "egg1 egg2 egg1 egg2") In this code, the first '(.+)' is the first capturing group. the second '(.+)' is the second capturing group. When this code is executed, it matches as follows The first (.+) matches the first 'egg1'. therefore, the '\1' matches second 'egg1'. The second (.+) matches the first 'egg2'. And, the '\2' matches the second 'egg2' In your code, You can not use '\2' because there is only one '(.+)'.
15th May 2017, 1:07 PM
Suyasa
Suyasa - avatar