Asked By – Daryl Spitzer
What is the difference between the
match() functions in the Python
I’ve read the documentation (current documentation), but I never seem to remember it. I keep having to look it up and re-learn it. I’m hoping that someone will answer it clearly with examples so that (perhaps) it will stick in my head. Or at least I’ll have a better place to return with my question and it will take less time to re-learn it.
Now we will see solution for issue: What is the difference between re.search and re.match?
re.match is anchored at the beginning of the string. That has nothing to do with newlines, so it is not the same as using
^ in the pattern.
As the re.match documentation says:
If zero or more characters at the
beginning of string match the regular expression pattern, return a
Noneif the string does not
match the pattern; note that this is
different from a zero-length match.
Note: If you want to locate a match
anywhere in string, use
re.search searches the entire string, as the documentation says:
Scan through string looking for a
location where the regular expression
pattern produces a match, and return a
Noneif no position in the
string matches the pattern; note that
this is different from finding a
zero-length match at some point in the
So if you need to match at the beginning of the string, or to match the entire string use
match. It is faster. Otherwise use
The documentation has a specific section for
search that also covers multiline strings:
Python offers two different primitive
operations based on regular
matchchecks for a match
only at the beginning of the string,
searchchecks for a match
anywhere in the string (this is what
Perl does by default).
matchmay differ from
even when using a regular expression
at the start of the string, or in
MULTILINEmode also immediately
following a newline. The “
operation succeeds only if the pattern
matches at the start of the string
regardless of mode, or at the starting
position given by the optional
argument regardless of whether a
newline precedes it.
Now, enough talk. Time to see some example code:
# example code: string_with_newlines = """something someotherthing""" import re print re.match('some', string_with_newlines) # matches print re.match('someother', string_with_newlines) # won't match print re.match('^someother', string_with_newlines, re.MULTILINE) # also won't match print re.search('someother', string_with_newlines) # finds something print re.search('^someother', string_with_newlines, re.MULTILINE) # also finds something m = re.compile('thing$', re.MULTILINE) print m.match(string_with_newlines) # no match print m.match(string_with_newlines, pos=4) # matches print m.search(string_with_newlines, re.MULTILINE) # also matches