What is the difference between the search() and match() functions in the Python re module?
I’ve read the Python 2 documentation (Python 3 documentation), but I never seem to remember it.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
re.matchis 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:
re.searchsearches the entire string, as the documentation says:So if you need to match at the beginning of the string, or to match the entire string use
match. It is faster. Otherwise usesearch.The documentation has a specific section for
matchvs.searchthat also covers multiline strings:Now, enough talk. Time to see some example code: