Python RegEX


Regular expressions, often called RegEx, give you a powerful way to search, match and work with text. They are used in many real applications like form validation, data cleaning, log analysis and pattern detection. Python has a built-in module called re that lets you use regular expressions easily.

In this tutorial, you will learn what RegEx is, how to use it in Python, how to search patterns, extract information, replace text and use important Metacharacters and special functions with practical examples.

What Is RegEx?

RegEx is a pattern used to match text. Instead of checking characters one by one, you define a pattern, and Python will find all text that fits that pattern.

For example:

  • Match email addresses

  • Validate phone numbers

  • Identify dates

  • Remove unwanted characters

  • Extract useful information from strings

RegEx helps you write cleaner and faster solutions for text-based tasks.

The re Module in Python

Python provides the re module for working with regular expressions.

import re

This module contains useful functions like:

  • search()

  • findall()

  • match()

  • sub()

  • split()

Each one helps you work with text in different ways.

Using findall() to Get All Matches

findall() returns all matches of a pattern in the text.

Example:

import re

text = "My ages were 15, 18 and now 20."
result = re.findall(r"\d+", text)

print(result)

Output:

['15', '18', '20']

\d+ finds one or more digits.

Using search() to Find the First Match

search() checks the text and returns the first match it finds.

Example:

import re

text = "Welcome to Delhi."
result = re.search("Delhi", text)

print(result.group())

group() returns the matched text.

Using match() to Check the Beginning of the String

match() works only if the pattern is at the start.

Example:

import re

text = "Hello World"
result = re.match("Hello", text)

print(result.group())

If the text does not start with the pattern, match() returns None.

Using split() to Break the Text

split() divides text based on a pattern.

import re

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

print(result)

Output:

['apple', 'banana', 'mango']

Using sub() to Replace Text

You can replace characters that match a pattern.

Example:

import re

text = "Aarohi is 20 years old."
result = re.sub(r"\d+", "XX", text)

print(result)

Output:

Aarohi is XX years old.

This is useful for masking sensitive data.

Understanding Metacharacters

Metacharacters help build powerful patterns. Here are the most common ones:

  • . → any character

  • ^ → starts with

  • $ → ends with

  • * → zero or more

  • + → one or more

  • ? → zero or one

  • {} → specific number of repeats

  • [] → match one character from a set

  • | → OR

  • () → group

Matching Digits and Letters

Python provides shorthand character classes:

  • \d → digits

  • \D → non-digits

  • \w → letters, digits and underscore

  • \W → non-word characters

  • \s → spaces

  • \S → non-spaces

Example:

import re

text = "User123 logged_in"
result = re.findall(r"\w+", text)

print(result)

Output:

['User123', 'logged_in']

Validating Phone Numbers

RegEx is widely used to validate input formats.

Example:

import re

phone = "9876543210"

if re.fullmatch(r"\d{10}", phone):
    print("Valid number")
else:
    print("Invalid")

This checks if the number has exactly 10 digits.

Extracting Email Addresses

Example:

import re

text = "Contact us at support@example.com or hello@testmail.org"

emails = re.findall(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}", text)

print(emails)

Output:

['support@example.com', 'hello@testmail.org']

This pattern is commonly used in many applications.

Using Groups

Groups help you capture specific parts of a pattern.

Example:

import re

text = "Name: Aarohi, Age: 20"

result = re.search(r"Name: (.*), Age: (\d+)", text)

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

Output:

Aarohi
20

Groups are helpful when you want to extract data separately.

Matching Dates

Example:

import re

date = "24-11-2025"

match = re.fullmatch(r"\d{2}-\d{2}-\d{4}", date)

print(bool(match))

This checks the DD-MM-YYYY format.

Real Life Example: Cleaning Text

Remove special characters:

import re

text = "Hello!!! Welcome### to??? Python."
clean = re.sub(r"[^a-zA-Z0-9 ]", "", text)

print(clean)

Output:

Hello Welcome to Python

This is useful for data cleaning tasks.

Practical Examples

  1. Find words starting with A

re.findall(r"\bA\w+", text)
  1. Check if text starts with a number

re.match(r"\d", text)
  1. Replace multiple spaces

re.sub(r"\s+", " ", text)
  1. Split at digits

re.split(r"\d", text)
  1. Find uppercase letters

re.findall(r"[A-Z]", text)
  1. Find lowercase letters

re.findall(r"[a-z]", text)
  1. Find numbers

re.findall(r"\d+", text)
  1. Find non-letters

re.findall(r"[^a-zA-Z]", text)
  1. Match username rules

re.fullmatch(r"[A-Za-z0-9_]{3,15}", username)
  1. Extract words of length 5

re.findall(r"\b\w{5}\b", text)

Summary of the Tutorial

RegEx gives you flexible control over text patterns. With Python’s re module, you can search, extract, validate and clean text efficiently. You learned how to use findall(), search(), match(), sub(), metacharacters, groups, character sets and common patterns for emails, phone numbers and dates. RegEx is used widely in data analysis, input validation and automation, making it an important skill for Python developers.


Practice Questions

Q1. Write a Python program to use re.search() to find the word "apple" in a sentence.

Q2. Write a Python program to use re.match() to check if a string starts with "Hello".

Q3. Write a Python program to use re.findall() to count how many times the word "dog" appears in a sentence.

Q4. Write a Python program to replace all numbers in a string with the "#" symbol using re.sub().

Q5. Write a Python program to split a string using a comma and space ", " as the separator using re.split().

Q6. Write a Python program to create a regex pattern to find all email addresses in a string.

Q7. Write a Python program to match all words starting with a capital letter using regex.

Q8. Write a Python program to use ^ and $ to check if a string starts and ends with specific words.

Q9. Write a Python program to match all 3-digit numbers from a string using a regex pattern like \b\d{3}\b.

Q10. Write a Python program to extract all words from a sentence using \w+.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top