Check if String Starts with Specific Word - Regex - Python
Python - Check if string starts with specific word using Regex
To check if a string starts with a word in Python, use the regular expression for "starts with" ^
and the word itself.
In this tutorial, we shall use the re.search()
function to perform an expression match against the string and determine whether the string starts with a given word or not.
Examples
1. Check if given string starts with a specific word
In the following example, we will check if the given string 'Python is a programming language.' starts with a specific word 'Python'.
Python Program
import re
str = 'Python is a programming language.'
#search using regex
x = re.search('^Python', str)
if(x!=None):
print('The line starts with \'Python\'.')
else:
print('The line does not start with \'Python\'.')
Explanation:
- The program starts by importing the
re
module. - The string
str
is defined as'Python is a programming language.'
. - We use
re.search('^Python', str)
to check if the string starts with 'Python'. The^
symbol ensures the match is at the beginning of the string. - If a match is found, the program prints that the line starts with 'Python'; otherwise, it prints that it does not.
Output
The line starts with 'Python'.
If you print the output of re.search()
, x, from the above example, you will get:
<re.Match object; span=(0, 6), match='Python'>
2. Negative scenario
In this example, we will check if the given string 'Python is a programming language.' starts with the word 'programming'.
Python Program
import re
str = 'Python is a programming language.'
#search using regex
x = re.search('^programming', str)
if(x!=None):
print('The line starts with \'programming\'.')
else:
print('The line does not start with \'programming\'.')
Explanation:
- The program uses the string
'Python is a programming language.'
. - The regex expression
^programming
searches for the word 'programming' at the start of the string. - Since the string starts with 'Python' and not 'programming',
re.search()
returnsNone
, and the program outputs that the line does not start with 'programming'.
Output
The line does not start with 'programming'.
If you print the output of re.search()
, x, from the above example, you will get:
None
If there are no matching instances for the regular expression provided, you will get None as the output for the re.search()
function.
3. Check if the string starts with a word (case-insensitive)
In this example, we will check if the string 'python is a programming language.' starts with the word 'python', ignoring case sensitivity.
Python Program
import re
str = 'python is a programming language.'
#search using regex with case-insensitive flag
x = re.search('^python', str, re.IGNORECASE)
if(x!=None):
print('The line starts with \'python\', case-insensitive check.')
else:
print('The line does not start with \'python\'.')
Explanation:
- The program uses the string
'python is a programming language.'
. - The
re.search()
function is used with there.IGNORECASE
flag, allowing it to ignore case while matching the word 'python'. - Since the string starts with 'python' (case-insensitive), the program will output that the line starts with 'python'.
Output
The line starts with 'python', case-insensitive check.
4. Check if the string starts with any of multiple words
In this example, we will check if the string starts with any of the words 'Python' or 'Java'.
Python Program
import re
str = 'Python is a programming language.'
#search using regex for multiple possible starting words
x = re.search('^(Python|Java)', str)
if(x!=None):
print('The line starts with \'Python\' or \'Java\'.')
else:
print('The line does not start with \'Python\' or \'Java\'.')
Explanation:
- The program uses the string
'Python is a programming language.'
. - The regular expression
'^(Python|Java)'
checks if the string starts with either 'Python' or 'Java'. - Since the string starts with 'Python', the program will output that the line starts with 'Python' or 'Java'.
Output
The line starts with 'Python' or 'Java'.
Summary
In this tutorial, we learned how to check if a string starts with a specific word using Python Regular Expressions. We covered multiple scenarios including case-insensitive checks, matching multiple starting words, and the negative case where the string does not match the given word.