Python Regular Expressions (RegEx)

← Back to Home

 

🔍 Python Regular Expressions (RegEx) Made Easy

📘 What is RegEx?

RegEx (Regular Expression) is a powerful tool used to search, match, and manipulate strings using patterns.

Think of RegEx as a special search language. Instead of just checking if a string contains "hello", you can check if it contains a valid email, a phone number, or even specific word patterns.

Python provides support for RegEx using the re module.


🛠️ Getting Started: Importing the re Module

import re

This gives you access to RegEx functions like search, match, findall, and sub.


🔎 Basic RegEx Functions in Python

Function Description
re.match() Checks for a match at the beginning
re.search() Searches the entire string
re.findall() Returns all matches as a list
re.sub() Replaces pattern matches with a string

📌 Common RegEx Patterns (Cheat Sheet)

Pattern Meaning Example Match
. Any character except newline     a.cabc, axc
^ Start of string     ^Hello
$ End of string     world$
* 0 or more repetitions     lo*looo
+ 1 or more repetitions     lo+loo
? 0 or 1 occurrence     colou?rcolor, colour
{n} Exactly n times     a{3}aaa
[abc] Any one of a, b, or c     b
\d Any digit (0–9)     1, 7, 0
\w Any alphanumeric character     a, 1, _
\s Any whitespace         space, tab, newline

✅ Practical Examples

1. Check if a string starts with a word

import re

text = "Python is awesome"
result = re.match(r"Python", text)
print(bool(result))  # True

2. Find all digits in a string

text = "My number is 123-456-7890"
digits = re.findall(r"\d+", text)
print(digits)  # ['123', '456', '7890']

3. Extract email addresses

text = "Contact us at support@example.com or info@domain.org"
emails = re.findall(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}", text)
print(emails)  # ['support@example.com', 'info@domain.org']

4. Replace all whitespace with hyphens

text = "This is a test"
new_text = re.sub(r"\s", "-", text)
print(new_text)  # This-is-a-test

5. Validate if a string is a phone number

phone = "123-456-7890"
pattern = r"^\d{3}-\d{3}-\d{4}$"
print(bool(re.match(pattern, phone)))  # True

🧠 When to Use RegEx

  • Validating inputs (email, phone numbers, usernames)

  • Extracting specific data from text

  • Text replacements and cleanups

  • Searching large text files or logs


⚠️ Tips for Using RegEx Safely

  • Test your patterns on small examples first.

  • Use raw strings r"pattern" in Python to avoid escaping backslashes.

  • Use online tools like regex101.com to test and explain patterns.


🚀 Final Thoughts

Regular expressions may seem tricky at first, but they’re an essential skill for any Python developer dealing with text, data validation, or web scraping. With practice, RegEx becomes a superpower that makes our code more efficient and capable.


No comments:

Post a Comment

Featured Post

AI Ethics and Future Trends: Navigating Challenges and Innovations in Artificial Intelligence

  🌐 Part 9: Ethics and Future Trends in Artificial Intelligence Why Ethics Matter in AI As AI becomes more powerful and widespread, e...

Popular Posts