I'm stuck with the following regular expression in Python 3.6.4:
import re
regex = r'\d{1,3}[-\s]?\d{3}[-\s]?\d{3}'
m = re.match(regex, '12377-456-789')
The output of the above code is:
<_sre.SRE_Match object; span=(0, 9), match='12377-456'>
The 7.2. re — Regular expression operations in the online Python documentation at:
https://docs.python.org/2/library/re.html#regular-expression-syntax
says the following:
{m} Specifies that exactly m copies of the previous RE should be matched; fewer matches cause the entire RE not to match. For example, a{6} will match exactly six 'a' characters, but not five.
{m,n} Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as many repetitions as possible.
Since the hyphen or space [-\s]? is optional after \d{1,3}, we don't have exactly 3 digits required by \d{3}, instead we have only two digits 77 followed by a hyphen. So how did Python return a match?
According to the official description, the regex should not match the string but surprisingly it does!
So I'm wondering as to how it is possible to get the above match output by Python.
Thanks a lot.