it seems that i got a bug in python: (Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32)
>>> re.match("0[5-7][5-9][0-9]{7}", "0775123456") #match
<_sre.SRE_Match object at 0x0000000002D3FC60>
>>> re.match("0[5-7][5-9][0-9]{7}", "077512345") #dont match
>>> re.match("0[5-7][5-9][0-9]{7}", "07751234567") #match!
<_sre.SRE_Match object at 0x0000000002D3F920>
>>> re.match("0[5-7][5-9][0-9]{7}","07751234567777777777777777777777777777777777777777777777") #match!!
<_sre.SRE_Match object at 0x0000000002D3FC60>
so i must use the dollar sign to limit “exactly” the numer!
i think it’s a bug, because in HTML5 validation, the {7} means “exactly” 7 digits, and here in python, it seems that it means “at least”
and here is how it behaves using the dollars sign:
>>> re.match("0[5-7][5-9][0-9]{7}$", "0775123456") #match
<_sre.SRE_Match object at 0x0000000002D3F920>
>>> re.match("0[5-7][5-9][0-9]{7}$", "07751234567") #dont match
>>> re.match("0[5-7][5-9][0-9]{7}$", "077512345") #dont match
and this happens to all other regular expression, not only the exact number matching, the dollar must be added!
so is it a bug? or it’s by design?
This is by design.
re.matchmatches at the beginning of a string as opposed tore.search, which matches anywhere in a string. Extra characters after the string are ignored. See http://docs.python.org/library/re.html#match for more details.Other languages that also use regular expressions, such as grep and perl, act the same. Regular expressions are primarily used for searching text.
If you want to perform an exact match you have to specify the dollar sign as you yourself also noted.