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:
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.
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.
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.
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.
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()
()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With