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
How does B regular expression work in Python?
Regular expressions (regex) are powerful tools for pattern matching in strings. Python's re module provides comprehensive support for working with regex patterns to search, match, and manipulate text data.
Understanding Regular Expressions in Python
Regular expressions are sequences of characters that define search patterns. They allow you to find specific patterns within strings, validate input formats, and perform complex text manipulations efficiently.
Importing the re Module
To use regular expressions in Python, you need to import the re module ?
import re
Common Regular Expression Methods
Python's re module provides several methods for working with patterns. Here are the most commonly used ones ?
Using re.search()
The re.search() function searches for the first occurrence of a pattern anywhere in the string ?
import re
# Searching for a simple pattern in a string
text = "Hello, world!"
pattern = "world"
# Use re.search
result = re.search(pattern, text)
if result:
print(f"Found: {result.group()}")
else:
print("Not found!")
Found: world
Using re.match()
The re.match() function checks if the string starts with the specified pattern ?
import re
# Checking if a string starts with a specific pattern
text = "Hello, Tutorialspoint!"
pattern = "Hello"
# Use re.match
result = re.match(pattern, text)
if result:
print("Match found!")
else:
print("No match found!")
Match found!
Using re.findall()
The re.findall() function returns all non-overlapping matches of a pattern in the string ?
import re
# Finding all digits in a string
text = "My number is 888 and my friend's number is 999."
# Pattern to find one or more digits
pattern = r'\d+'
# Use re.findall to get all digit occurrences
numbers = re.findall(pattern, text)
print(f"Found numbers: {numbers}")
Found numbers: ['888', '999']
Using re.sub()
The re.sub() function replaces all occurrences of a pattern with a replacement string ?
import re # Replace all vowels with asterisks text = "Hello, Everyone!" # Pattern to find all vowels (case-insensitive) pattern = r'[aeiouAEIOU]' # Use re.sub to replace vowels new_text = re.sub(pattern, '*', text) print(new_text)
H*ll*, *v*ry*n*!
Common Pattern Symbols
| Symbol | Description | Example |
|---|---|---|
\d |
Any digit (0-9) |
\d+ matches "123" |
\w |
Any word character |
\w+ matches "hello" |
\s |
Any whitespace |
\s+ matches spaces |
. |
Any character (except newline) |
a.c matches "abc" |
+ |
One or more occurrences |
a+ matches "aa" |
* |
Zero or more occurrences |
a* matches "" or "aaa" |
Conclusion
Regular expressions in Python provide powerful pattern matching capabilities through the re module. Use re.search() to find patterns, re.match() to check string beginnings, re.findall() to get all matches, and re.sub() for replacements.
