Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

regex

This 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 returns none.

I tried taking the brackets off,

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

It still returns none.

My questions are:

  1. Why the re.match does not find anything?
  2. What is the difference with or without bracket in pattern?
like image 468
bing Avatar asked Feb 18 '13 10:02

bing


People also ask

How does Python re match work?

match() function of re in Python will search the regular expression pattern and return the first occurrence. The Python RegEx Match method checks for a match only at the beginning of the string. So, if a match is found in the first line, it returns the match object.

Is there any difference between re match () and re search () in the Python re module?

There is a difference between the use of both functions. Both return the first match of a substring found in the string, but re. match() searches only from the beginning of the string and return match object if found.

What does re match () return?

The match method returns a corresponding match object instance if zero or more characters at the beginning of the string match the regular expression pattern. In simple words, the re. match returns a match object only if the pattern is located at the beginning of the string; otherwise, it will return None.

How do you repeat a regular expression?

A repeat is an expression that is repeated an arbitrary number of times. An expression followed by '*' can be repeated any number of times, including zero. An expression followed by '+' can be repeated any number of times, but at least once.


1 Answers

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()
()
like image 137
eumiro Avatar answered Sep 20 '22 05:09

eumiro