Python Regular Expressions (RegEx) Made Easy

← Back to Home

Mastering Python RegEx: A Complete Guide



What is RegEx?

RegEx (Regular Expression) is a powerful tool for searching, matching, and manipulating text using patterns.

Think of RegEx as a special search language. Instead of just checking for a literal word, you can match patterns like:

  • Valid emails
  • Phone numbers
  • Specific word structures

Python provides RegEx support via the re module.



Getting Started: Import the re Module

import re

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



Basic RegEx Functions in Python

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


📌 Common RegEx Patterns Cheat Sheet

Pattern Meaning Example Match
.Any character except newlinea.cabc, axc
^Start of string^Hello
$End of stringworld$
*0 or more repetitionslo*looo
+1 or more repetitionslo+loo
?0 or 1 occurrencecolou?rcolor, colour
{n}Exactly n timesa{3}aaa
[abc]Any one of a, b, or cb
\dAny digit (0–9)1, 7
\wAny alphanumeric charactera, 1, _
\sWhitespace characterspace, tab, newline


Practical Examples

1. Check if a string starts with a word

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 a phone number format

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


When to Use RegEx

  • Input validation (emails, phone numbers, usernames)
  • Extracting specific data from text
  • Text replacement or cleanup
  • Searching large text files or logs


Tips for Using RegEx Safely

  • Test patterns on small examples first.
  • Use raw strings r"pattern" to avoid escaping backslashes.
  • Try online tools like regex101.com for testing and debugging patterns.


Final Thoughts

Regular expressions may seem tricky initially, but they are an essential skill for Python developers dealing with text, validation, or data extraction. With practice, RegEx becomes a superpower that simplifies and strengthens your code.