This article is all about the start of line ^ and end of line $ regular expressions in Python’s re library.
These two regexes are fundamental to all regular expressions—even outside the Python world. So invest 5 minutes now and master them once and for all!
You can also listen to the video as you scroll through the post. It’ll make learning much easier:
Related article: Python Regex Superpower – The Ultimate Guide
Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.
Python Re Start-of-String (^) Regex
You can use the caret operator ^ to match the beginning of the string. For example, this is useful if you want to ensure that a pattern appears at the beginning of a string. Here’s an example:
>>> import re >>> re.findall('^PYTHON', 'PYTHON is fun.') ['PYTHON']
The findall(pattern, string) method finds all occurrences of the pattern in the string. The caret at the beginning of the pattern ‘^PYTHON’ ensures that you match the word Python only at the beginning of the string. In the previous example, this doesn’t make any difference. But in the next example, it does:
>>> re.findall('^PYTHON', 'PYTHON! PYTHON is fun') ['PYTHON']
Although there are two occurrences of the substring ‘PYTHON’, there’s only one matching substring—at the beginning of the string.
But what if you want to match not only at the beginning of the string but at the beginning of each line in a multi-line string? In other words:
Python Re Start-of-Line (^) Regex
The caret operator, per default, only applies to the start of a string. So if you’ve got a multi-line string—for example, when reading a text file—it will still only match once: at the beginning of the string.
However, you may want to match at the beginning of each line. For example, you may want to find all lines that start with ‘Python’ in a given string.
You can specify that the caret operator matches the beginning of each line via the re.MULTILINE flag. Here’s an example showing both usages—without and with setting the re.MULTILINE flag:
>>> import re >>> text = ''' Python is great. Python is the fastest growing major programming language in the world. Pythonistas thrive.''' >>> re.findall('^Python', text) [] >>> re.findall('^Python', text, re.MULTILINE) ['Python', 'Python', 'Python'] >>>
The first output is the empty list because the string ‘Python’ does not appear at the beginning of the string.
The second output is the list of three matching substrings because the string ‘Python’ appears three times at the beginning of a line.
Python re.sub()
The re.sub(pattern, repl, string, count=0, flags=0) method returns a new string where all occurrences of the pattern in the old string are replaced by repl. Read more in the Finxter blog tutorial.
You can use the caret operator to substitute wherever some pattern appears at the beginning of the string:
>>> import re >>> re.sub('^Python', 'Code', 'Python is \nPython') 'Code is \nPython'
Only the beginning of the string matches the regex pattern so you’ve got only one substitution.
Again, you can use the re.MULTILINE flag to match the beginning of each line with the caret operator:
>>> re.sub('^Python', 'Code', 'Python is \nPython', flags=re.MULTILINE) 'Code is \nCode'
Now, you replace both appearances of the string ‘Python’.
Python re.match(), re.search(), re.findall(), and re.fullmatch()
Let’s quickly recap the most important regex methods in Python:
- The re.findall(pattern, string, flags=0) method returns a list of string matches. Read more in our blog tutorial.
- The re.search(pattern, string, flags=0) method returns a match object of the first match. Read more in our blog tutorial.
- The re.match(pattern, string, flags=0) method returns a match object if the regex matches at the beginning of the string. Read more in our blog tutorial.
- The re.fullmatch(pattern, string, flags=0) method returns a match object if the regex matches the whole string. Read more in our blog tutorial.
You can see that all four methods search for a pattern in a given string. You can use the caret operator ^ within each pattern to match the beginning of the string. Here’s one example per method:
>>> import re >>> text = 'Python is Python' >>> re.findall('^Python', text) ['Python'] >>> re.search('^Python', text) <re.Match object; span=(0, 6), match='Python'> >>> re.match('^Python', text) <re.Match object; span=(0, 6), match='Python'> >>> re.fullmatch('^Python', text) >>>
So you can use the caret operator to match at the beginning of the string. However, you should note that it doesn’t make a lot of sense to use it for the match() and fullmatch() methods as they, by definition, start by trying to match the first character of the string.
You can also use the re.MULTILINE flag to match the beginning of each line (rather than only the beginning of the string):
>>> text = '''Python is Python''' >>> re.findall('^Python', text, flags=re.MULTILINE) ['Python', 'Python'] >>> re.search('^Python', text, flags=re.MULTILINE) <re.Match object; span=(0, 6), match='Python'> >>> re.match('^Python', text, flags=re.MULTILINE) <re.Match object; span=(0, 6), match='Python'> >>> re.fullmatch('^Python', text, flags=re.MULTILINE) >>>
Again, it’s questionable whether this makes sense for the re.match() and re.fullmatch() methods as they only look for a match at the beginning of the string.
Python Re End of String ($) Regex
Similarly, you can use the dollar-sign operator $ to match the end of the string. Here’s an example:
>>> import re >>> re.findall('fun$', 'PYTHON is fun') ['fun']
The findall() method finds all occurrences of the pattern in the string—although the trailing dollar-sign $ ensures that the regex matches only at the end of the string.
This can significantly alter the meaning of your regex as you can see in the next example:
>>> re.findall('fun$', 'fun fun fun') ['fun']
Although, there are three occurrences of the substring ‘fun’, there’s only one matching substring—at the end of the string.
But what if you want to match not only at the end of the string but at the end of each line in a multi-line string?
Python Re End of Line ($)
The dollar-sign operator, per default, only applies to the end of a string. So if you’ve got a multi-line string—for example, when reading a text file—it will still only match once: at the end of the string.
However, you may want to match at the end of each line. For example, you may want to find all lines that end with ‘.py’.
To achieve this, you can specify that the dollar-sign operator matches the end of each line via the re.MULTILINE flag. Here’s an example showing both usages—without and with setting the re.MULTILINE flag:
>>> import re >>> text = ''' Coding is fun Python is fun Games are fun Agreed?''' >>> re.findall('fun$', text) [] >>> re.findall('fun$', text, flags=re.MULTILINE) ['fun', 'fun', 'fun'] >>>
The first output is the empty list because the string ‘fun’ does not appear at the end of the string.
The second output is the list of three matching substrings because the string ‘fun’ appears three times at the end of a line.
Python re.sub()
The re.sub(pattern, repl, string, count=0, flags=0) method returns a new string where all occurrences of the pattern in the old string are replaced by repl. Read more in the Finxter blog tutorial.
You can use the dollar-sign operator to substitute wherever some pattern appears at the end of the string:
>>> import re >>> re.sub('Python$', 'Code', 'Is Python\nPython') 'Is Python\nCode'
Only the end of the string matches the regex pattern so there’s only one substitution.
Again, you can use the re.MULTILINE flag to match the end of each line with the dollar-sign operator:
>>> re.sub('Python$', 'Code', 'Is Python\nPython', flags=re.MULTILINE) 'Is Code\nCode'
Now, you replace both appearances of the string ‘Python’.
Python re.match(), re.search(), re.findall(), and re.fullmatch()
All four methods—re.findall(), re.search(), re.match(), and re.fullmatch()—search for a pattern in a given string. You can use the dollar-sign operator $ within each pattern to match the end of the string. Here’s one example per method:
>>> import re >>> text = 'Python is Python' >>> re.findall('Python$', text) ['Python'] >>> re.search('Python$', text) <re.Match object; span=(10, 16), match='Python'> >>> re.match('Python$', text) >>> re.fullmatch('Python$', text) >>>
So you can use the dollar-sign operator to match at the end of the string. However, you should note that it doesn’t make a lot of sense to use it for the fullmatch() methods as it, by definition, already requires that the last character of the string is part of the matching substring.
You can also use the re.MULTILINE flag to match the end of each line (rather than only the end of the whole string):
>> text = '''Is Python Python''' >>> re.findall('Python$', text, flags=re.MULTILINE) ['Python', 'Python'] >>> re.search('Python$', text, flags=re.MULTILINE) <re.Match object; span=(3, 9), match='Python'> >>> re.match('Python$', text, flags=re.MULTILINE) >>> re.fullmatch('Python$', text, flags=re.MULTILINE) >>>
As the pattern doesn’t match the string prefix, both re.match() and re.fullmatch() return empty results.
How to Match the Caret (^) or Dollar ($) Symbols in Your Regex?
You know that the caret and dollar symbols have a special meaning in Python’s regular expression module: they match the beginning or end of each string/line. But what if you search for the caret (^) or dollar ($) symbols themselves? How can you match them in a string?
The answer is simple: escape the caret or dollar symbols in your regular expression using the backslash. In particular, use ‘\^’ instead of ‘^’ and ‘\$’ instead of ‘$’. Here’s an example:
>>> import re >>> text = 'The product ^^^ costs $3 today.' >>> re.findall('\^', text) ['^', '^', '^'] >>> re.findall('\$', text) ['$']
By escaping the special symbols ^ and $, you tell the regex engine to ignore their special meaning.
Regex Humor
Where to Go From Here?
You’ve learned everything you need to know about the caret operator ^ and the dollar-sign operator $ in this regex tutorial.
Summary: The caret operator ^ matches at the beginning of a string. The dollar-sign operator $ matches at the end of a string. If you want to match at the beginning or end of each line in a multi-line string, you can set the re.MULTILINE flag in all the relevant re methods.
Want to earn money while you learn Python? Average Python programmers earn more than $50 per hour. You can become average, can’t you?
Join the free webinar that shows you how to become a thriving coding business owner online!
[Webinar] Are You a Six-Figure Freelance Developer?
Join us. It’s fun! ๐
Python Regex Course
Google engineers are regular expression masters. The Google search engine is a massive text-processing engine that extracts value from trillions of webpages.ย ย
Facebook engineers are regular expression masters. Social networks like Facebook, WhatsApp, and Instagram connect humans via text messages.ย
Amazon engineers are regular expression masters. Ecommerce giants ship products based on textual product descriptions.ย ย Regular expressions โrule the game โwhen text processing โmeets computer science.ย
If you want to become a regular expression master too, check out the most comprehensive Python regex course on the planet: