Python Regular Expressions (Regex) – Complete Guide with Examples

Introduction

Regular Expressions are powerful pattern-matching tools that allow developers to search and manipulate text efficiently. Python provides built-in support for Regular Expressions through the re module.

Regex is widely used in:

  • Form validation
  • Data cleaning
  • Web scraping
  • Log file analysis
  • Search engines
  • Text processing
  • Natural Language Processing (NLP)

By learning Regex, you can perform complex string operations with just a few lines of code.

What are Regular Expressions (Regex)?

A Regular Expression (Regex) is a sequence of characters that defines a search pattern.

In simple words:

Regex is a pattern used to match, search, and manipulate text.

For example:


\d

matches any digit.

[a-z] matches any lowercase letter.

Regex allows Python to identify patterns instead of exact text.

Why Use Regex?

Regular Expressions provide several benefits.

1. Fast Text Searching

Find specific patterns quickly.

2. Data Validation

Validate emails, phone numbers, passwords, etc.

3. Data Extraction

Extract useful information from large text.

4. Text Replacement

Replace unwanted content efficiently.

5. Flexible Pattern Matching

Handle dynamic and complex text formats.

Importing the re Module

Python’s Regex functionality is available through the re module.


import re

Before using Regex functions, import the module.

Basic Regex Syntax

Example:


import re
text = "Python is awesome"
result = re.search(
    "Python",
    text
)
print(result)

Output:

<re.Match object>

The pattern “Python” is found in the string.

Common Regex Functions

Python’s re module provides several useful functions.

Function Purpose
re.search() Finds first match
re.match() Matches at beginning
re.findall() Returns all matches
re.finditer() Returns iterator of matches
re.sub() Replaces matches
re.split() Splits string
re.compile() Compiles regex pattern

re.search()

The search() function searches the entire string.

Example:


import re
text = "Welcome to Python"
result = re.search(
    "Python",
    text
)
print(result.group())

Output:

Python

re.match()

The match() function checks only the beginning of the string.

Example:


import re
text = "Python Programming"
result = re.match(
    "Python",
    text
)
print(result.group())

Output:

Python

Example:


re.match(
    "Programming",
    text
)

Output:

None

Because the pattern is not at the beginning.

re.findall()

Returns all matching occurrences.

Example:


import re
text = "Python Java Python C++"
result = re.findall(
    "Python",
    text
)
print(result)

Output:

[‘Python’, ‘Python’]

re.finditer()

Returns match objects one by one.

Example:


import re
text = "Python Python"
matches = re.finditer(
    "Python",
    text
)
for match in matches:
    print(match.start())

Output:

0
7

re.sub()

Used for replacing text.

Example:


import re
text = "I love Java"
result = re.sub(
    "Java",
    "Python",
    text
)
print(result)

Output:


I love Python

re.split()

Splits text using a regex pattern.

Example:


import re
text = "apple,banana,mango"
result = re.split(
    ",",
    text
)
print(result)

Output:

[‘apple’, ‘banana’, ‘mango’]

Special Regex Characters

Regex uses special symbols called metacharacters.

Symbol Meaning
. Any character
^ Start of string
$ End of string
* Zero or more
+ One or more
? Zero or one
[] Character set
{} Repetition count
| OR operator
() Grouping

Character Classes

Match Digits


\d

Example:


import re
text = "Age: 25"
print(
    re.findall(
        r"\d",
        text
    )
)

Output:

[‘2’, ‘5’]

Match Non-Digits


\D

Example:


re.findall(
    r"\D",
    "123ABC"
)

Output:

[‘A’, ‘B’, ‘C’]

Match Word Characters


\w

Matches:

  1. Letters
  2. Digits
  3. Underscores

Example:


re.findall(
    r"\w",
    "Python_3"
)

Output:


['P','y','t','h','o','n','_','3']

Match Whitespace


\s

Example:


re.findall(
    r"\s",
    "Python Tutorial"
)

Output:

[‘ ‘]

Quantifiers

Quantifiers define repetition.

*

Zero or more occurrences.


ab*

Matches:


a
ab
abb
abbb

?

Zero or one occurrence.


ab?

Matches:


a
ab

{}

Specific repetitions.

Example:


\d{4}

Matches exactly four digits.

Example:


re.findall(
    r"\d{4}",
    "Year 2025"
)

Output:

[‘2025’]

Anchors

Anchors define positions.

Start Anchor (^)

Example:


re.match(
    r"^Python",
    "Python Tutorial"
)

Matches because the string starts with Python.

End Anchor ($)

Example:


re.search(
    r"Tutorial$",
    "Python Tutorial"
)

Matches because the string ends with Tutorial.

Character Sets

Example:


[a-z]

Matches lowercase letters.


[A-Z]

Matches uppercase letters.


[0-9]

Matches digits.

Example:


re.findall(
    r"[A-Z]",
    "Python Regex"
)

Output:


['P', 'R']

Real-Life Examples:

1. Email Validation


import re
email = "user@gmail.com"
pattern = r"^[\w\.-]+@[\w\.-]+\.\w+$"
if re.match(
    pattern,
    email
):
    print("Valid Email")
else:
    print("Invalid Email")

Output:


Valid Email

2. Phone Number Validation


import re
phone = "9876543210"
pattern = r"^\d{10}$"
if re.match(
    pattern,
    phone
):
    print("Valid")

Output:

Valid

3. Extracting URLs


import re
text = """
Visit https://example.com
and https://google.com
"""
urls = re.findall(
    r"https://\S+",
    text
)
print(urls)

Output:

[
‘https://example.com’,
‘https://google.com’
]

4. Password Validation


import re
password = "Python@123"
pattern = (
    r"^(?=.*[A-Z])"
    r"(?=.*[a-z])"
    r"(?=.*\d)"
    r".{8,}$"
)
if re.match(
    pattern,
    password
):
    print("Strong Password")

Output:

Strong Password

Compiling Regex Patterns

For repeated use, compile patterns.

Example:


import re
pattern = re.compile(
    r"\d+"
)
result = pattern.findall(
    "10 20 30"
)
print(result)

Output:

[’10’, ’20’, ’30’]

Benefits:

  • Better performance
  • Cleaner code

Advantages of Regex

Advantage Description
Powerful Searching Finds complex patterns
Data Validation Validates user input
Efficient Extraction Extracts useful information
Text Manipulation Replaces and formats text
Widely Supported Available in many languages

Disadvantages of Regex

Disadvantage Description
Difficult Syntax Can be hard to learn
Reduced Readability Complex patterns are confusing
Debugging Challenges Errors may be difficult to find
Overuse Simple string methods may be better

Common Mistakes

1. Forgetting Raw Strings

Incorrect:


"\d+"

Correct:


r"\d+"

Raw strings prevent escape character issues.

2. Using match() Instead of search()

Incorrect:


re.match(
    "Python",
    "Learn Python"
)

Output:


None

Use re.search() instead.

3. Writing Overly Complex Patterns

Complicated regex patterns reduce readability.

4. Not Escaping Special Characters

Incorrect:


".com"

Correct:


"\.com"

5. Ignoring Case Sensitivity

Regex is case-sensitive by default.

Incorrect:

Example:


re.search(
    "python",
    "Python"
)

Output:

None

Use re.IGNORECASE if needed.

Conclusion

Python Regular Expressions (Regex) provide a powerful and flexible way to search, validate, extract, and manipulate text. Using Python’s built-in re module, developers can perform complex pattern matching operations with minimal code.

Regex is widely used in form validation, data extraction, web scraping, log analysis, and text processing applications.

Related Python Tutorials