Regular Expressions (RegEx)
What is Regular Expression?
Regular Expression (RegEx) is a technique used to search, match, and manipulate text based on patterns.
In Python, RegEx is provided by re module.
import re
Why RegEx is Important?
- Validating emails, phone numbers
- Extracting IPs, URLs from logs
- Checking passwords
- Searching patterns
- Replacing characters
- Parsing large text files
Important Functions in re Module
| Function | Description |
|---|---|
re.search() |
Returns first match |
re.match() |
Match only beginning of string |
re.findall() |
Returns all matches |
re.split() |
Split string using pattern |
re.sub() |
Replace text |
Basic Patterns (Metacharacters)
| Symbol | Meaning | |
|---|---|---|
. |
Any character | |
^ |
Start of string | |
$ |
End of string | |
* |
Zero or more times | |
+ |
One or more times | |
? |
Zero or one | |
{n} |
n times | |
{n,m} |
n to m times | |
[] |
Character set | |
\ |
Escape character | |
| ` | ` | OR |
Character Classes
| Pattern | Meaning |
|---|---|
\d |
Digits (0–9) |
\D |
Non-digit |
\w |
Word (letters, numbers, _) |
\W |
Non-word |
\s |
Space |
\S |
Non-space |
re.search() Example
import re
text = "Python is awesome"
result = re.search("awesome", text)
print(result)
Returns match object if found
re.findall() Example
import re
text = "cat rat mat fat"
print(re.findall("at", text))
Output:
['at', 'at', 'at', 'at']
re.match() Example
import re
print(re.match("Hello", "Hello World"))
Matches only at start.
re.split() Example
import re
text = "one,two,three"
print(re.split(",", text))
re.sub() Example (Replace)
import re
text = "Python is easy"
print(re.sub("easy", "powerful", text))
Pattern Matching Examples
1.Find all digits
re.findall(r"\d", "A1B2C3")
2.Find words starting with capital letter
re.findall(r"[A-Z][a-z]*", "India USA china Japan")
Email Validation Pattern
import re
email = "user123@gmail.com"
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$'
if re.match(pattern, email):
print("Valid email")
else:
print("Invalid email")
Phone Number Validation (10 digits)
import re
num = "9876543210"
if re.fullmatch(r"\d{10}", num):
print("Valid Phone Number")
Password Validation
- At least 8 characters
- One uppercase
- One number
import re
pwd = "Abcd1234"
pattern = r"^(?=.[A-Z])(?=.\d).{8,}$"
if re.match(pattern, pwd):
print("Strong Password")
Extract IP Address
import re
log = "Error from 192.168.1.1 at 10:22 PM"
ip = re.findall(r"\d+.\d+.\d+.\d+", log)
print(ip)
Extract All Words
re.findall(r"\w+", "Python is super fast!")
Special Example: Find Vowels
re.findall(r"[aeiouAEIOU]", "Beautiful")
DevOps Use Case: Extract Error Lines from Log
import re
log = """
INFO: Started
ERROR: Disk Not Found
WARNING: Low Memory
ERROR: Service Down
errors = re.findall(r"ERROR.*", log)
print(errors)
Top comments (0)