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
Python – Program that matches a word containing ‘g’ followed by one or more e’s using regex
Regular expressions (regex) in Python provide powerful pattern matching capabilities. In this tutorial, we'll learn how to match words containing 'g' followed by one or more 'e' characters using different regex methods.
Understanding the Regex Pattern
The pattern r'\bge+\w*\b' breaks down as follows ?
\bWord boundary to match complete wordsgLiteral character 'g'e+One or more 'e' characters\w*Zero or more word characters (optional)\bWord boundary at the end
Method 1: Using findall() Function
The findall() method returns all non-overlapping matches as a list ?
import re
# Sample text with words containing 'g' followed by 'e'
text = "green geese gather energy in the garden"
# Pattern to match 'g' followed by one or more 'e'
pattern = r'\bge+\w*\b'
# Find all matches
matches = re.findall(pattern, text)
print("Matches found:", matches)
Matches found: ['green', 'geese']
Method 2: Using finditer() Function
The finditer() method returns an iterator of match objects with additional information ?
import re
text = "green geese gather energy in the garden"
pattern = r'\bge+\w*\b'
# Find all matches with positions
matches = re.finditer(pattern, text)
for match in matches:
print(f"Match: '{match.group()}' at position {match.start()}-{match.end()}")
Match: 'green' at position 0-5 Match: 'geese' at position 6-11
Method 3: Using search() Function
The search() method finds the first occurrence of the pattern ?
import re
text = "The green geese are beautiful"
pattern = r'\bge+\w*\b'
# Find first match
first_match = re.search(pattern, text)
if first_match:
print(f"First match: '{first_match.group()}' at position {first_match.start()}")
else:
print("No match found")
First match: 'green' at position 4
Complete Example with Multiple Patterns
import re
def find_g_e_words(text):
"""Find words starting with 'g' followed by one or more 'e' characters"""
pattern = r'\bge+\w*\b'
matches = re.findall(pattern, text, re.IGNORECASE)
return matches
# Test with different texts
test_texts = [
"The green geese flew over the gentle breeze",
"Geography and geometry are interesting subjects",
"Glee and geek are common words"
]
for i, text in enumerate(test_texts, 1):
matches = find_g_e_words(text)
print(f"Text {i}: {text}")
print(f"Matches: {matches}")
print()
Text 1: The green geese flew over the gentle breeze Matches: ['green', 'geese', 'gentle'] Text 2: Geography and geometry are interesting subjects Matches: ['Geography', 'geometry'] Text 3: Glee and geek are common words Matches: ['Glee', 'geek']
Comparison of Methods
| Method | Returns | Best For |
|---|---|---|
findall() |
List of strings | Getting all matches as strings |
finditer() |
Iterator of match objects | Position information needed |
search() |
First match object | Finding only the first occurrence |
Conclusion
Python's re module provides multiple methods to match patterns like 'g' followed by 'e' characters. Use findall() for simple string results, finditer() when you need position information, and search() for the first match only.
