Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can I match the start and end in Python\'s regex?
In Python, the re module is used to work with regular expressions. In some cases, we need to check whether a string starts or ends with a specific pattern, and then we need to use special regex characters called anchors. Following are the anchors used in regular expressions −
- ^ − This anchor matches the beginning of a string.
- $ − This anchor matches the end of a string.
Python Regex Methods
The following methods from the re module are commonly used to apply start and end matching −
- re.match(): Checks for a match only at the beginning of the string.
- re.search(): Scans the entire string for a match.
- re.fullmatch(): Matches the entire string against the pattern.
Match a String that Starts with a Word
To match a string that starts with a specific word, we need to place the "^" symbol before the word and pass it as a pattern to the re.match() method.
Since "^" matches the beginning of the string, this pattern matches a string only if it starts with the specified word ?
import re
text = "Welcome to Tutorialspoint"
if re.match(r"^Welcome", text):
print("Starts with 'Welcome'")
else:
print("Does not start with 'Welcome'")
Starts with 'Welcome'
Match a String that Ends with a Word
Similarly, to match a string that ends with a specific word, we need to place the "$" symbol after the word and pass it as a pattern to the re.search() method.
Since "$" matches the end of the string, this pattern matches a string only if it ends with the specified word ?
import re
text = "Learn Python Programming"
if re.search(r"Programming$", text):
print("Ends with 'Programming'")
else:
print("Does not end with 'Programming'")
Ends with 'Programming'
Match an Entire String Using "^" and "$"
If we want to match an entire string exactly, from the beginning to the end, we can combine the anchors "^" and "$". If the pattern is the desired word/regex placed between these two symbols, the re.match() matches a string that starts and ends with the specified pattern ?
import re
text = "admin123"
pattern = r"^admin\d{3}$"
if re.match(pattern, text):
print("Exact pattern matched")
else:
print("No match")
Exact pattern matched
Match Lines Individually Using re.MULTILINE
When we are working with multi-line strings and we want to match each single line individually, then we can use ^ and $ anchors to match each line by using the re.MULTILINE flag ?
import re text = "Start now\nStart again\nFinish later" matches = re.findall(r"^Start", text, re.MULTILINE) print(matches)
['Start', 'Start']
Using re.fullmatch() for Exact String Match
The re.fullmatch() method in Python accepts a pattern and string as parameters and matches the given pattern in the specified string. In case of a match, it returns the match object and returns None if there are no matches.
The re.match() or re.search() methods match only part of the string, whereas, re.fullmatch() method only returns a match if the whole string satisfies the pattern ?
import re
text = "Python3.11"
pattern = r"Python3\.\d{2}"
if re.fullmatch(pattern, text):
print("Full match found")
else:
print("No full match")
Full match found
Comparison of Methods
| Method | Matches | Best For |
|---|---|---|
re.match() |
From start only | String beginning patterns |
re.search() |
Anywhere in string | Finding patterns anywhere |
re.fullmatch() |
Entire string | Complete string validation |
Conclusion
Use ^ and $ anchors to match string start and end positions. The re.fullmatch() method provides the most precise matching for complete string validation.
