Python re.match regex, why doesn't this code work?

It is written in Python,

import re s='1 89059809102/30589533 IronMan 30 Santa Ana Massage table / IronMan 30 Santa Ana Massage table' pattern='\s(\d{11})/(\d{8})' re.match(pattern,s) 

it does not return anything.

I tried to remove the brackets,

 pattern='\s\d{11}/\d{8}' 

It still returns none .

My questions:

  • Why doesn't re.match find anything?
  • What is the difference with or without a template in a template?
+8
python regex
source share
1 answer

re.match "matches" from the beginning of the line, but there is an extra 1 .

Instead, use re.search , which will "search" anywhere on the string. And, in your case, also find something:

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

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

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

All Articles