Python regular expression re.match, why this code does not work? [duplicate]

re.match “matches” since the beginning of the string, but there is an extra 1.

Use re.search instead, which will “search” anywhere within the string. And, in your case, also find something:

>>> re.search(pattern,s).groups()
('89059809102', '30589533')

If you remove the brackets in pattern, it will still return a valid _sre.SRE_Match, object, but with empty groups:

>>> re.search('\s\d{11}/\d{8}',s).groups()
()

Leave a Comment