Regular Expression (Regex) in Python
Regular Expressions (Regex) in Python are used to search, match, extract, and manipulate text based on patterns. Regex is widely used in data validation, log analysis, automation testing, and text processing.
Why Use Regular Expressions?
- Validate user input (email, phone, password)
- Search and extract text patterns
- Replace or clean unwanted data
- Parse logs and reports
Regex Module in Python
Python provides the built-in re module for working with regular expressions.
import re
Basic Regex Functions
| Function | Description |
|---|---|
| re.match() | Matches pattern at beginning |
| re.search() | Searches pattern anywhere |
| re.findall() | Returns all matches |
| re.finditer() | Returns iterator of matches |
| re.sub() | Replaces matched text |
re.match()
import re
text = "Python is powerful"
result = re.match("Python", text)
if result:
print("Match found")
re.search()
text = "Learning Python Regex"
result = re.search("Python", text)
if result:
print("Found at position:", result.start())
re.findall()
text = "Python Java Python Selenium"
result = re.findall("Python", text)
print(result)
re.sub()
text = "My phone number is 9876543210"
new_text = re.sub(r"\d", "*", text)
print(new_text)
Common Regex Patterns
| Pattern | Description |
|---|---|
| . | Any character except newline |
| \d | Digit (0-9) |
| \D | Non-digit |
| \w | Word character |
| \W | Non-word character |
| \s | Whitespace |
| \S | Non-whitespace |
Quantifiers
| Symbol | Meaning |
|---|---|
| * | 0 or more |
| + | 1 or more |
| ? | 0 or 1 |
| {n} | Exactly n times |
Character Sets
[a-z] # lowercase letters
[A-Z] # uppercase letters
[0-9] # digits
Validating Email Using Regex
email = "test@example.com"
pattern = r"^[\w\.-]+@[\w\.-]+\.\w+$"
if re.match(pattern, email):
print("Valid Email")
else:
print("Invalid Email")
Regex Groups
text = "Order ID: 12345"
match = re.search(r"(\d+)", text)
print(match.group())
Flags in Regex
| Flag | Description |
|---|---|
| re.I | Ignore case |
| re.M | Multiline |
| re.S | Dot matches newline |
Regex in Automation Testing
import re
response = "Order ID: 98765 confirmed"
order_id = re.search(r"\d+", response).group()
print(order_id)
Used in:
- Validating API responses
- Extracting dynamic data
- Log file analysis
Best Practices
- Use raw strings (r"...")
- Keep patterns simple and readable
- Test regex patterns before use
- Use comments for complex regex
Common Interview Questions
- What is regex?
- Difference between match and search?
- What is re.findall()?
- What are regex groups?
- What are regex flags?
Conclusion
Regular Expressions in Python provide powerful text processing capabilities. They are essential for data validation, automation testing, and real-time applications.
👉 Learn more Python and Automation tutorials on way2testing.com
No comments:
Post a Comment