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 corresponding MatchObject instance. Return None if 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 search() instead.

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 corresponding MatchObject instance. Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.

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 search.

The documentation has a specific section for match vs. search that also covers multiline strings:

Python offers two different primitive operations based on regular expressions: match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string (this is what Perl does by default).

Note that match may differ from search even when using a regular expression beginning with '^': '^' matches only at the start of the string, or in MULTILINE mode also immediately following a newline. The โ€œmatchโ€ 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 pos 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
Answer from nosklo on Stack Overflow
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ re.html
re โ€” Regular expression operations
4 days ago - However, if Python would recognize the resulting sequence, the backslash should be repeated twice. This is complicated and hard to understand, so itโ€™s highly recommended that you use raw strings for all but the simplest expressions. ... Used to indicate a set of characters. In a set: Characters can be listed individually, e.g. [amk] will match 'a', 'm', or 'k'.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ re-match-in-python
re.match() in Python - GeeksforGeeks
July 23, 2025 - re.match method in Python is used to check if a given pattern matches the beginning of a string. Itโ€™s like searching for a word or pattern at the start of a sentence. For example, we can use re.match to check if a string starts with a certain ...
Discussions

python - What is the difference between re.search and re.match? - Stack Overflow
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. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do you use re.match() ? Python
Can you give an example of a string you're trying to match? More on reddit.com
๐ŸŒ r/learnprogramming
8
1
August 16, 2022
Regular Expressions (RE) Module - Search and Match Comparison
Hello, I have a question regarding the regular expression compile. I created a code snippet to compare the different search and match results using different strings and using different patterns. Here is the test code snippet: import re s1 = 'bob has a birthday on Feb 25th' s2 = 'sara has a ... More on discuss.python.org
๐ŸŒ discuss.python.org
0
October 26, 2023
regex - How can I find all matches to a regular expression in Python? - Stack Overflow
If you are interested in getting ... matches. The tool supports a more general language of regular expressions with captures, called REQL. For instance, the regexp !x{...} will give all triples of three contiguous characters (including overlapping triples). The approach should be more efficient that @cottontail's answer (which is general quadratic in the input string). You can try REmatch out online here and get the Python code ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_regex.asp
Python RegEx
Python has a built-in package called re, which can be used to work with Regular Expressions. ... You can add flags to the pattern when using regular expressions. A special sequence is a \ followed by one of the characters in the list below, and has a special meaning: A set is a set of characters inside a pair of square brackets [] with a special meaning: The findall() function returns a list containing all matches.
Top answer
1 of 10
687

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 corresponding MatchObject instance. Return None if 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 search() instead.

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 corresponding MatchObject instance. Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.

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 search.

The documentation has a specific section for match vs. search that also covers multiline strings:

Python offers two different primitive operations based on regular expressions: match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string (this is what Perl does by default).

Note that match may differ from search even when using a regular expression beginning with '^': '^' matches only at the start of the string, or in MULTILINE mode also immediately following a newline. The โ€œmatchโ€ 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 pos 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
2 of 10
146

search โ‡’ find something anywhere in the string and return a match object.

match โ‡’ find something at the beginning of the string and return a match object.

๐ŸŒ
Built In
builtin.com โ€บ articles โ€บ python-re-match
Python re.match() and re.sub() Explained | Built In
Pythonโ€™s re.match() and re.sub() are two methods from its re module. re.sub() is a function that substitutes occurrences of a pattern in a string, while re.match() is a function that checks for a match at the beginning of a string.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ howto โ€บ regex.html
Regular Expression HOWTO โ€” Python 3.14.3 documentation
Regular expressions (called REs, or regexes, or regex patterns) are essentially a tiny, highly specialized programming language embedded inside Python and made available through the re module.
Find elsewhere
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ regular expressions โ€บ re.match()
Python | Regular Expressions | re.match() | Codecademy
September 5, 2023 - The following example returns a match object (<re.Match object; span=(0, 12), match='123-456-7890'>) and not None since the phone number (123-456-7890) matches the test pattern: ... Learn the basics of regular expressions and how to pull and clean data from the web with Python.
๐ŸŒ
TestDriven.io
testdriven.io โ€บ tips โ€บ 421e050b-176b-4a72-a8b5-6ad5f185b86a
Tips and Tricks - Difference between re.search and re.match in Python? | TestDriven.io
re.match() searches for matches from the beginning of a string while re.search() searches for matches anywhere in the string. ... import re claim = 'People love Python.' print(re.search(r'Python', claim).group()) # => Python print(re.match(...
๐ŸŒ
Google
developers.google.com โ€บ google for education โ€บ python โ€บ python regular expressions
Python Regular Expressions | Python Education | Google for Developers
In Python a regular expression ... and searches for that pattern within the string. If the search is successful, search() returns a match object or None otherwise....
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ how do you use re.match() ? python
r/learnprogramming on Reddit: How do you use re.match() ? Python
August 16, 2022 -

Iโ€™ve been looking at tutorials and I canโ€™t figure out why I canโ€™t get it to work, Iโ€™m trying to see if a substring matches a certain format but the output is incorrect.

re.match(โ€˜[A-Z],[A-Z] $โ€™ , s)

Is this not how to set it up?

๐ŸŒ
Basic Python
astuntechnology.github.io โ€บ python-basics โ€บ regular-expressions.html
Python Regular Expressions | Basic Python
In Python a regular expression search is typically written as: ... The re.search() method takes a regular expression pattern and a string and searches for that pattern within the string. If the search is successful, search() returns a match object or None otherwise.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Regular Expressions (RE) Module - Search and Match Comparison - Python Help - Discussions on Python.org
October 26, 2023 - Hello, I have a question regarding the regular expression compile. I created a code snippet to compare the different search and match results using different strings and using different patterns. Here is the test code snippet: import re s1 = 'bob has a birthday on Feb 25th' s2 = 'sara has a birthday on March 3rd' s3 = '12eup 586iu' s4 = '0turt' # '\w\w\w \d\d\w\w' bday1_re = re.compile('\w+ \d+\w+') # Also tried: '\w+ \d+\w+' bday2_re = re.comp...
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ regex-regular-expressions
Mimo: The coding platform you need to learn Web Development, Python, and more.
It's best practice to define regex patterns using raw strings by prefixing the string with an r (e.g., r"\d+") to prevent backslashes from being misinterpreted. 1. Finding the First Match with re.search(): This function returns a match object if the pattern is found, and None otherwise.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-rematch-method-in-python
What is the re.match method in Python?
This function only checks for a match at the beginning of the string. This means that re.match() will return the match found in the first line of the string, but not those found in any other line, in which case it will return null.
๐ŸŒ
Jobsity
jobsity.com โ€บ blog โ€บ how-to-use-regular-expressions-in-python
How to Use Regular Expressions in Python
In this tutorial, Iโ€™m going to discuss the three most practical and widely used functions. The re.match() function of the re module in Python searches for the regular expression pattern and returns the first occurrence.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-re-search-vs-re-match
re.search() vs re.match() - Python - GeeksforGeeks
July 12, 2025 - Explanation: re.search(r"error", "Server error at 5:00 PM") looks for the substring "error" anywhere in the text and since it appears after "Server", it finds a match and returns a match object. Example 2: Finding the first occurrence of 'apple' anywhere in the string. ... Explanation: re.search(r"apple", "I have an apple and an orange.") searches for the first occurrence of the substring "apple" in the string.
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
`re.match()`: raise exception if string doesn't match? - Ideas - Discussions on Python.org
December 28, 2021 - Is there any sense in providing a version of re.match() that raises an exception, rather than returning None, when the string doesnโ€™t match the pattern? Itโ€™s one of the few places where I find myself repeatedly writing iโ€ฆ
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Regular Expressions in Python: the re Module | note.nkmk.me
May 9, 2023 - In Python, the re module allows you to work with regular expressions (regex) to extract, replace, and split strings based on specific patterns. re โ€” Regular expression operations โ€” Python 3.11.3 doc ...