0

In "More Metacharacters", third explanation page, what is that "(-)" doing there?

In "More Metacharacters", third explanation page, what is that "(-)" doing there? The specific lesson is about the question mark metacharacter, but there's a "(-)" in the string that really got me confused. The metacharacter ? means "zero or one repetitions". Example: 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")

30th Jul 2019, 12:22 AM
Pedro Carneiro Jr
Pedro Carneiro Jr - avatar
2 odpowiedzi
+ 1
"Causes the resulting RE to match 0 or 1 repetitions of the preceding RE. ab? will match either 'a' or 'ab'." https://docs.python.org/3/library/re.html#index-5
30th Jul 2019, 1:22 AM
Diego
Diego - avatar
+ 1
Thanks, Diego! And here is an online RegEx tester that I found very useful: https://regex101.com/ All the best!
3rd Aug 2019, 1:31 AM
Pedro Carneiro Jr
Pedro Carneiro Jr - avatar