PyLearn Learn Python Professionally
Home / Lessons / Python Regular Expressions
Lesson 53 · Advanced Python Concepts

Python Regular Expressions

Learn how to search, validate, extract, replace, and transform text using Python's powerful re module and regular expression patterns.

50–60 minutes Intermediate Advanced Python Concepts
0%
01

Learning Objectives

By the end of this lesson, you will understand how regular expressions work and how to use Python's re module for practical text-processing tasks.

You will learn how to:

  • Understand the purpose of regular expressions.
  • Import and use Python's re module.
  • Search text using patterns.
  • Use character classes and quantifiers.
  • Use anchors such as ^ and $.
  • Capture information using groups.
  • Extract multiple matches.
  • Split text using regular expressions.
  • Replace matching text.
  • Build practical validation and extraction patterns.
02

What Is a Regular Expression?

A regular expression, commonly called regex or regexp, is a pattern used to search and manipulate text.

Instead of looking for one exact string, you can describe a pattern that many strings can match.

For example, suppose you want to find every number in a piece of text.

text = "Order 125 contains 3 products and costs $450"

A regular expression can identify the numbers without knowing their exact values beforehand.

\d+

Matches

125
3
450
03

Why Regular Expressions Matter

Regular expressions are particularly useful when working with large amounts of text.

Common applications

  • Email validation
  • Phone-number extraction
  • Finding URLs
  • Extracting prices
  • Searching log files
  • Cleaning text
  • Extracting dates
  • Validating IDs
  • Parsing structured text

Regex is especially valuable in data cleaning, automation, web scraping, log analysis, and text processing.

04

Python's re Module

Python provides regular expression functionality through the built-in re module.

import re

You do not need to install a separate package to use the standard regular expression features.

Some of the most important functions are:

  • re.search()
  • re.match()
  • re.fullmatch()
  • re.findall()
  • re.finditer()
  • re.split()
  • re.sub()
06

re.match()

re.match() checks whether the pattern matches at the beginning of the string.

import re


text = "Python programming"


result = re.match("Python", text)


if result:
    print("Matched at the beginning")

But:

result = re.match("programming", text)

print(result)
None
Remember:

search() looks anywhere in the string, while match() checks the beginning.

07

re.fullmatch()

re.fullmatch() requires the entire string to match the pattern.

import re


result = re.fullmatch(r"\d+", "12345")


if result:
    print("Valid number")
Valid number

But:

result = re.fullmatch(r"\d+", "12345abc")

print(result)
None

This makes fullmatch() useful when you want strict validation.

08

Basic Regular Expression Patterns

Regular expressions use special symbols to describe patterns.

Important symbols

  • \d — digit
  • \D — non-digit
  • \w — word character
  • \W — non-word character
  • \s — whitespace
  • \S — non-whitespace
  • . — almost any character
import re


text = "Room 25"


print(re.findall(r"\d", text))
print(re.findall(r"\d+", text))
print(re.findall(r"\w+", text))
['2', '5']
['25']
['Room', '25']
09

Character Classes

Character classes let you define which characters can appear at a particular position.

Square brackets

[abc]

This matches one character that is either a, b, or c.

Ranges

[a-z]
[A-Z]
[0-9]

These represent lowercase letters, uppercase letters, and digits respectively.

Negated character class

[^0-9]

The caret inside the brackets means "anything except" the listed characters.

10

Quantifiers

Quantifiers specify how many times a pattern should occur.

  • * — zero or more
  • + — one or more
  • ? — zero or one
  • {n} — exactly n times
  • {n,} — at least n times
  • {n,m} — between n and m times
import re


text = "Python 123"


print(re.findall(r"\d+", text))
['123']

The + combines consecutive digits into one match.

11

Anchors

Anchors describe positions in a string rather than actual characters.

  • ^ — beginning of string
  • $ — end of string
  • \b — word boundary
  • \B — non-word boundary
import re


text = "Python"


print(bool(re.search(r"^Python", text)))
print(bool(re.search(r"Python$", text)))
True
True
12

Capturing Groups

Parentheses create groups that allow you to capture specific parts of a match.

import re


text = "Name: Ali, Age: 25"


pattern = r"Name: (\w+), Age: (\d+)"

result = re.search(pattern, text)


if result:

    print(result.group(1))
    print(result.group(2))
Ali
25

You can also access the complete match using:

result.group(0)

Groups are extremely useful when extracting structured information from text.

13

re.findall()

re.findall() returns all non-overlapping matches as a list.

import re


text = "Python 101, Java 202, SQL 303"


numbers = re.findall(r"\d+", text)

print(numbers)
['101', '202', '303']

This is one of the most commonly used regex functions for extracting information.

14

re.finditer()

re.finditer() returns match objects one at a time.

import re


text = "Order 125 costs 450"


for match in re.finditer(r"\d+", text):

    print(match.group())
    print(match.start())
    print(match.end())
125
6
9
450
16
19

This is useful when you need additional information about each match, such as its position.

15

re.split()

re.split() splits a string wherever the regular expression matches.

import re


text = "apple,banana;orange|mango"


items = re.split(r"[,;|]", text)

print(items)
['apple', 'banana', 'orange', 'mango']

This is particularly useful when input data uses inconsistent separators.

16

re.sub()

re.sub() replaces text matching a pattern.

import re


text = "Python is amazing"


cleaned = re.sub(r"amazing", "powerful", text)

print(cleaned)
Python is powerful

You can use regex patterns to perform more advanced replacements.

import re


text = "Phone: 0300-1234567"


masked = re.sub(r"\d", "*", text)

print(masked)
Phone: ****-*******
17

Regex Flags

Flags modify how Python interprets a regular expression.

re.IGNORECASE

import re


text = "Python PYTHON python"


matches = re.findall(
    r"python",
    text,
    re.IGNORECASE
)

print(matches)
['Python', 'PYTHON', 'python']

re.MULTILINE

re.MULTILINE changes how ^ and $ behave when working with multiple lines.

re.DOTALL

re.DOTALL allows the dot . to match newline characters as well.

18

Escaping Special Characters

Some characters have special meanings in regular expressions.

If you want to match the literal character, you may need to escape it.

For example, a period normally means "any character".

.

To match an actual period:

\.

Other commonly escaped characters include:

\+
\*
\?
\(
\)
\[
\]
\{ 
\}
19

Raw Strings

Regex patterns often contain backslashes, so Python programmers commonly use raw strings.

pattern = r"\d+"

instead of:

pattern = "\\d+"

The r prefix tells Python to treat the string as a raw string, making regex patterns easier to read and write.

Professional habit:

Use raw strings for most regex patterns that contain backslashes.

20

Real-World Regex Examples

Extracting Email Addresses

import re


text = """
Contact ali@example.com
or sara@example.org
"""


emails = re.findall(
    r"[\w.-]+@[\w.-]+\.\w+",
    text
)

print(emails)
['ali@example.com', 'sara@example.org']

Extracting Phone Numbers

import re


text = "Call 0300-1234567 or 0312-7654321"


phones = re.findall(
    r"\d{4}-\d{7}",
    text
)

print(phones)
['0300-1234567', '0312-7654321']

Extracting Prices

import re


text = "Laptop $1200, Phone $800, Mouse $25"


prices = re.findall(
    r"\$\d+",
    text
)

print(prices)
['$1200', '$800', '$25']

Finding Dates

import re


text = "Meeting dates: 12-08-2026 and 25-09-2026"


dates = re.findall(
    r"\d{2}-\d{2}-\d{4}",
    text
)

print(dates)
['12-08-2026', '25-09-2026']
21

Common Regex Mistakes

Mistake 1 — Confusing search() and match()

search() can find a match anywhere, while match() starts from the beginning.

Mistake 2 — Forgetting raw strings

Backslashes can become confusing when ordinary Python strings are used for complex regex patterns.

Mistake 3 — Using overly broad patterns

A pattern such as .* can match far more than intended.

Mistake 4 — Assuming regex is always the best solution

Simple string methods such as startswith(), endswith(), split(), and replace() may be clearer for simple tasks.

Important:

Do not use regex simply because it is powerful. Use the simplest tool that clearly solves the problem.

22

Practice Exercises

Exercise 1 — Extract Numbers

Given a sentence containing several numbers, use re.findall() to extract every number.

Exercise 2 — Extract Emails

Find every email address from a paragraph of text.

Exercise 3 — Find Phone Numbers

Extract Pakistani-style phone numbers from a block of text.

Exercise 4 — Replace Sensitive Data

Replace all digits in a phone number with *.

Exercise 5 — Extract Product Prices

Find all dollar prices from a product description.

Exercise 6 — Validate a Code

Use re.fullmatch() to check whether a product code follows a format such as PROD-1234.

23

Mini Challenge — Smart Contact Extractor

Build a small text-analysis program that extracts useful contact information from a business description.

import re


text = """
Contact our sales team:

Email: sales@example.com
Support: support@example.com

Phone: 0300-1234567
Alternative: 0312-7654321

Website: https://www.example.com
"""


emails = re.findall(
    r"[\w.-]+@[\w.-]+\.\w+",
    text
)

phones = re.findall(
    r"\d{4}-\d{7}",
    text
)

urls = re.findall(
    r"https?://[^\s]+",
    text
)


print("Emails:", emails)
print("Phones:", phones)
print("URLs:", urls)

Expected Output

Emails: ['sales@example.com', 'support@example.com']

Phones: ['0300-1234567', '0312-7654321']

URLs: ['https://www.example.com']

Extend the Challenge

  1. Extract all dates.
  2. Extract all prices.
  3. Count the number of email addresses.
  4. Replace phone numbers with [HIDDEN].
  5. Create a reusable function called analyze_text().
  6. Return all extracted information as a dictionary.

This challenge combines regex searching, extraction, replacement, functions, and structured data into a practical text-processing application.

24

Knowledge Check

Test your understanding of Python regular expressions.

1. Which Python module provides regular expressions?

2. What does \d generally represent?

3. Which function searches anywhere in a string for the first match?

4. Which function returns all matching strings as a list?

5. What does the + quantifier mean?

6. What does ^ generally represent?

7. Which function replaces matching text?

8. Why are raw strings commonly used for regex?

9. Which function requires the entire string to match?

10. When should you avoid regex?

25

Lesson Summary

Regular expressions provide a powerful way to search, extract, validate, split, and replace structured text.

Key Concepts

  • Regular expressions describe text patterns.
  • Python provides regex through the built-in re module.
  • re.search() searches anywhere for the first match.
  • re.match() checks the beginning of the string.
  • re.fullmatch() requires the entire string to match.
  • re.findall() extracts all matches.
  • re.finditer() provides match objects and positions.
  • re.split() splits text using a pattern.
  • re.sub() replaces matching text.
  • Character classes define allowed characters.
  • Quantifiers define how many times something can occur.
  • Groups allow you to capture specific portions of text.
  • Raw strings make regex patterns easier to read.

You now have a powerful tool for text processing and data extraction. In the next lesson, we move into Type Hints, where you will learn how to make Python code clearer, easier to maintain, and easier for development tools to understand.

Finished this lesson?

Mark this lesson complete when you are comfortable creating and using regular expression patterns.