Python – Program that matches a word containing ‘g’ followed by one or more e’s using regex


Python is the most powerful programming language to work in the field of data visualization and deep learning. The regex also known as regular expressions is an effective tool to search for any characters or text in a given document. Python language provides the user with versatile roles ranging from basic calculations to complex ones. The Python methods use various functionalities to match a word in the given text. Python is a high-level programming and a versatile one that is mostly preferred by the developers and also it is widely used for data analysis.

The program that matches a word using regex

To explain this, we can take an example like take a variable named text with certain values.

Text = “John is a genius at playing football games”.

The output is printed as [genius, game]

Regular Expression

  • The `r` at the beginning of the string indicates that it should be handled as raw input so special characters are not escaped when compiling the pattern.

  • The `\b` characters define word boundaries ensuring only complete words with `"g"`s preceded without letters will be located.

  • Each g-words appearing in spaces where there may exist more than one consecutive e-character must end on another boundary \b character provided after them; all other repeated (one OK- Zeroth inclusive) e-characters captured between definite word-patterns have been counted beforehand returning just matching phrases hereafter stored into matches object format_list datatype.

Approach:-

  • Approach 1 − Using the findall() function

  • Approach 2 − Using the match function

  • Approach 3 − Using the finditer() function

Approach 1: Python Program to match a word using regex with help of findall() function

The sentence is initialized with a set of words and the words starting with ‘g’ and followed by one or more e’s can be found using the findall method.

Algorithm

  • Step 1 − The “re” module is imported to use the regular expression value

  • Step 2 − The regular expression is stored in the variable named “pattern”.

  • Step 3 − The findall() function is defined with two parameters as pattern and sentence with the help of regular expression.

  • Step 4 − Finally, the statement is printed.

Example

#importing the re module
import re
#the input is initialized with a word starting with g and followed by one or two e’s
sentence = "geetanjali, gear, gemma, hello"
#Initializing the variable to store the value
pattern = r'\bg\w*e+\w*\b'
#Using the findall function, to match the pattern with the sentence
matching = re.findall(pattern, sentence)
#The list is returned with the string values
print("Words which are matching from the input:",matching)

Output

Words which are matching from the input: ['geetanjali', 'gear', 'gemma']

Approach 2: Python Program to match a word using regex with help of match() function

The method used to find all occurrences of the word in the given sentence is the match() method.

Algorithm

  • Step 1 − The required module to use the regular expression is imported.

  • Step 2 − The function is defined with two parameters.

  • Step 3 − When there are no matches, it returns the empty list.

  • Step 4 − The final list is printed.

Example

#importing the re module
import re
#the input is initialized with word starting with g and followed by one or two e’s
sentence = "geetanjali, gear, gemma, hello"
#Initializing the variable to store the value
pattern = r'\bg\w*e+\w*\b'
#function is defined with two parameters
def matchword(pattern, sentence):
    if not sentence:
        return []
    match = re.match(pattern, sentence)
    if match:
        return [match.group()] + matchword(pattern, sentence[match.end():])
    else:
        return matchword(pattern, sentence[1:])

matching = matchword(pattern, sentence)
#The list is returned with the string values
print("Words which are matching from the input:",matching)

Output

Words which are matching from the input: ['geetanjali', 'gear', 'gemma']

Approach 3: Python Program to match a word using regex with help of finditer() function

Compared to the findall method, the finditer() is used to print all the matches in the given sentences starting with ‘g’ and followed by one or two e’s.

Algorithm

  • Step 1 − The required “re” module is imported to use the regular expression value.

  • Step 2 − The input is initialized and composed of various words.

  • Step 3 − The regular expression is stored in the variable named “pattern”.

  • Step 4 − The findall() function is defined with two parameters as pattern and sentence with the help of regular expression

  • Step 5 − Finally, the statement is printed.

Example

#importing the re module
import re
#the input is initialized with a word starting with g and followed by one or two e’s
sentence = "geek, gear, gemma, hello"
#Initializing the variable to store the value
pattern = r'\bg\w*e+\w*\b'
#Using the finditer function, to match the pattern with the sentence
matches = re.finditer(pattern, sentence)
#for loop is used to iterate through the sentence
for match in matches:
    print(match.group())

Output

geek
gear
gemma

Conclusion

One common use for regex is to search for words that contain a certain sequence of characters, such as "g" followed by one or more e’s. This pattern can easily be matched using Python and its built-in regular expression module. The various approaches to match the occurrences of the word are explained using various methods.

Updated on: 04-Sep-2023

35 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements