exemple in the python course "regular expressions - More Metacharacters" | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

exemple in the python course "regular expressions - More Metacharacters"

I just read this exemple for the "?" meaning: import re pattern = r"ice(-)?cream" if re.match(pattern, "ice-cream"): print("Match 1") if re.match(pattern, "icecream"): print("Match 2") if re.match(pattern, "sausages"): print("Match 3") if re.match(pattern, "ice--ice"): print("Match 4") I think the "Match 4" would be with the string "ice--cream" and not with "ice--ice", isn't it?

31st May 2017, 9:16 AM
Adrien Bossi
Adrien Bossi - avatar
2 Answers
+ 1
your pattern will match ice-cream icecream (-)? read it as zero or one of the symbol - Best to experiment on a site like https://regex101.com/ for python or javascript regex or http://regexr.com/ for javascript regex or for general guidance http://www.rexegg.com/ or for fun https://regexcrossword.com/
31st May 2017, 1:30 PM
richard
0
I understand that but I just would talk about the (probably) mistake in the 4th exemple: the pattern could be r"ice(-)?cream", r"ice(-)+cream" or r"ice(-)*cream", that could never match with "ice--ice" :)
1st Jun 2017, 12:57 PM
Adrien Bossi
Adrien Bossi - avatar