Check if String ends with Specific Word - Regex - Python
Python Regex - Check if String Ends with Specific Word
To check if a string ends with a word in Python, use the regular expression for "ends with" $ and the word itself before $. The dollar sign ($
) asserts that the match must occur at the end of the string. We will use the re.search()
function to perform this check.
The regular expression to check if a string ends with a specific word looks like this:
'theword$'
Examples
1. Check if given string ends with the word "cherry"
In this example, we check if the string 'apple banana cherry' ends with the word 'cherry'.
Python Program
import re
str = 'apple banana cherry'
#search using regex
x = re.search('cherry$', str)
if(x!=None):
print('The line ends with \'cherry\'.')
else:
print('The line does not end with \'cherry\'.')
Explanation:
- The string
str
is defined as'apple banana cherry'
. - The
re.search()
function is used with the pattern'cherry$'
, which checks if the string ends with 'cherry'. - If a match is found, the program outputs that the line ends with 'cherry'; otherwise, it indicates that the line does not end with 'cherry'.
Output
The line ends with 'cherry'.
If you print the output of re.search()
, x, from the above example, you will get:
<re.Match object; span=(13, 19), match='cherry'>
2. Negative Scenario
In this example, we check if the string 'apple banana cherry'
ends with the word 'banana'
.
Python Program
import re
str = 'apple banana cherry'
#search using regex
x = re.search('banana$', str)
if(x!=None):
print('The line ends with \'banana\'.')
else:
print('The line does not end with \'banana\'.')
Explanation:
- The string
str
is defined as'apple banana cherry'
. - The
re.search()
function is used with the pattern'banana$'
, which checks if the string ends with 'banana'. - Since the string ends with 'cherry' and not 'banana',
re.search()
returnsNone
, and the program outputs that the line does not end with 'banana'.
Output
The line does not end with 'banana'.
If you print the output of re.search()
, x, from the above example, you will get:
None
3. Case-insensitive Check
In this example, we check if the string ends with the word 'CHERRY', ignoring case sensitivity.
Python Program
import re
str = 'apple banana cherry'
#search using regex with case-insensitive flag
x = re.search('CHERRY$', str, re.IGNORECASE)
if(x!=None):
print('The line ends with \'CHERRY\', case-insensitive check.')
else:
print('The line does not end with \'CHERRY\'.')
Explanation:
- The string
str
is defined as'apple banana cherry'
. - The
re.search()
function is used with the pattern'CHERRY$'
and there.IGNORECASE
flag, which ignores case while performing the match. - Since the string ends with 'cherry', the program outputs that the line ends with 'CHERRY' (case-insensitive check).
Output
The line ends with 'CHERRY', case-insensitive check.
4. Check if the String Ends with Any of Multiple Words
Here, we check if the string ends with either 'cherry' or 'banana'.
Python Program
import re
str = 'apple banana cherry'
#search using regex for multiple possible ending words
x = re.search('(cherry|banana)$', str)
if(x!=None):
print('The line ends with \'cherry\' or \'banana\'.')
else:
print('The line does not end with \'cherry\' or \'banana\'.')
Explanation:
- The string
str
is defined as'apple banana cherry'
. - The
re.search()
function uses the pattern'(cherry|banana)$'
, which checks if the string ends with either 'cherry' or 'banana'. - Since the string ends with 'cherry', the program outputs that the line ends with 'cherry' or 'banana'.
Output
The line ends with 'cherry' or 'banana'.
Summary
In this tutorial, we learned how to check if a string ends with a specific word or substring using Python Regular Expressions. We explored multiple scenarios, including case-insensitive checks and matching multiple possible ending words. With the re.search()
function and appropriate regex patterns, you can effectively handle these scenarios in your Python programs.