Python RegEx (Regular Expressions)
RegEx stands for Regular Expression – a powerful tool used to search, match, or manipulate text using a pattern.
Think of it as a smart "search tool" that can find things like :
- Email addresses
- Phone numbers
- Specific words or patterns in a string
Why Use RegEx in Python?
- Validate user inputs (email, phone, etc.)
- Search specific patterns in text
- Replace or split strings based on patterns
- Scrape structured data from messy text
Importing RegEx Module
Python provides the re module to work with regular expressions :
Basic RegEx Functions in Python
| Function |
Description |
re.search( ) |
Searches for the first match in a string |
re.match( ) |
Matches the pattern only at the beginning |
re.findall( ) |
Returns a list of all matches |
re.finditer( ) |
Returns an iterator of match objects |
re.sub( ) |
Replaces matches with a string |
re.split( ) |
Splits a string using a pattern |
Common Regex Patterns
| Pattern |
Meaning |
Example Match |
. |
Any character except newline |
a.c → matches "abc" |
^ |
Start of string |
^Hello → "Hello world" |
$ |
End of string |
world$ → "Hello world" |
\d |
Digit (0–9) |
\d\d\d → "123" |
\D |
Non-digit |
\D → "A" |
\w |
Word character (a–z, A–Z, 0–9, _) |
\w+ → "Python3" |
\s |
Whitespace (space, tab, newline) |
\s → matches spaces, tabs |
[] |
Set of characters |
[aeiou] → vowels |
+ |
One or more |
a+ → "aaa" |
* |
Zero or more |
a* → "", "aa" |
? |
Zero or one |
colou?r → "color", "colour" |
{n} |
Exactly n times |
\d{3} → "123" |
` |
` |
OR |
() |
Grouping |
(ab)+ → "abab" |
Examples
1. Search for a word
import re
text = "Python is powerful"
match = re.search("power", text)
print(match.group()) # Output: power
2. Find all digits
text = "Order ID: 453, Price: $49"
numbers = re.findall(r'\d+', text)
print(numbers) # Output: ['453', '49']
3. Replace text
text = "I like Java"
new_text = re.sub("Java", "Python", text)
print(new_text) # Output: I like Python
4. Validate Email (basic pattern)
email = "test@example.com"
pattern = r'^\w+@\w+\.\w+$'
print(bool(re.match(pattern, email))) # Output: True
Advanced Example – Find Phone Numbers
text = "Contact: 987-654-3210 or 123-456-7890"
matches = re.findall(r'\d{3}-\d{3}-\d{4}', text)
print(matches)
# Output: ['987-654-3210', '123-456-7890']
Summary
- re module = Python’s regular expression engine.
- Use patterns to match, search, replace, or split strings.
- Helpful for input validation, web scraping, data cleaning.