String and Line Anchors in Regular Expressions and Python
Today we are going to take a look at the string and line anchors in regular expressions, and also how to use these in python. An anchor will match comparing the beginning and/or ends of a string.
The anchor at the beginning of a string is the symbol ^. The anchor at the end of a string is $.
If we want to match a string that ends in a number, and don’t care what’s before that number, our regular expression would look like this:
.*[0-9]$
In the pattern, .* matches with any character while [0-9]$ ensures the string ends with a number.
Now let’s talk about the opposite. Let’s say we want a match when the string starts with a number. Then the regular expression would look like this.
^[0-9].*
This would match with this string:
5 asdf
Line Anchors with Regular Expressions in Python
Let’s say we have a string in python with multiple lines. And we want a match with each line that ends in a number.
import re
test = '''
this will match 1
this will also match 2
This 3 will not match
'''
# the third parameter makes it multiline
results = re.findall(r'.*[0-9]$', test, re.M)
print(results)
The thing to take note here, for python to know it’s a multiline, we pass in the flag re.M as the third parameter. Without it, it will only match with the first line in the string.
Want more content? Check out my YouTube Channel!
0 Comments