
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Example
Following is the example in which we are using the re.match() method to match a string that starts with a word by using the anchor ^ -
import re text = "Welcome to Tutorialspoint" if re.match(r"^Welcome", text): print("Starts with 'Welcome'") else: print("Does not start with 'Welcome'")
Here is the output of the above program -
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.match() method.
Since "$" matches the end of the string, this pattern matches a string only if it ends with the specified word.
Example
In the following example, we are using the re.search() method to match a string that ends with a word by using the anchor $ -
import re text = "Learn Python Programming" if re.search(r"Programming$", text): print("Ends with 'Programming'") else: print("Does not end with 'Programming'")
Here is the output of the above program -
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.
Example
In this example, we will see how to use the re.match() method to check if the complete string matches a specific pattern using both ^ and $ anchors -
import re text = "admin123" pattern = r"^admin\d{3}$" if re.match(pattern, text): print("Exact pattern matched") else: print("No match")
Here is the output of the above program -
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.
Example
In here we use the re.MULTILINE flag of the re.findall() method to get the match with each line -
import re text = "Start now\nStart again\nFinish later" matches = re.findall(r"^Start", text, re.MULTILINE) print(matches)
Following is the output of the above program -
['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.
Example
Here is the example in which we are using the re.fullmatch() method to check if the entire string matches the pattern exactly -
import re text = "Python3.11" pattern = r"Python3\.\d{2}" if re.fullmatch(pattern, text): print("Full match found") else: print("No full match")
Here is the output of the above program -
Full match found