special sequences | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

special sequences

mport re pattern = r"(.+) \1" match = re.match(pattern, "word word word wrod") 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") for the above code, i am getting the output match 1 match 2, is this regex only for two words only. i cant understand? if possible any of us can help me to understand

8th May 2020, 12:37 AM
Senthil Kumar
Senthil Kumar - avatar
1 Answer
+ 2
It is regex for [starting from the beginning of the string] any sequence of characters, followed by a space, then the same sequence of characters. "word word" will be matched because "word", space, "word" matched the pattern, same with "?! ?!". "abc cde" did not because no sequence of characters starting from the beginning of the string matched the pattern.
8th May 2020, 12:51 AM
Russ
Russ - avatar