Skip to main content

Python Regular Expressions (Regex): Complete Guide with Examples

 

✍️Introduction

Regular Expressions (Regex) are used to search, match, and manipulate text.
Python provides the re module to work with regular expressions.

Regular Expressions (Regex) define patterns that describe sets of strings and allow powerful text processing operations.

In Python, the re module supports pattern matching, searching, replacing, and validating text, making it useful for tasks like data validation, log analysis, and text parsing.

Regex is widely used in:

  • Form validation

  • Searching text

  • Data cleaning

  • Pattern matching


Import re Module:

The re module in Python is used to work with regular expressions for searching, matching, and manipulating text patterns.

To use regular expressions in a Python program, the re module must be imported first.
This is done by writing import re at the beginning of the program, which gives access to all regex-related functions such as searching, matching, and replacing text based on patterns.


What is a Regular Expression?

A Regular Expression (Regex) is a sequence of characters that defines a search pattern used to match, locate, or replace text within strings.

Regex is especially useful for validating input and extracting specific information from large text data.

For example, regex can be used to:

  • Find email addresses

  • Validate phone numbers

  • Extract usernames or IDs

  • Search for specific words or formats in text files

In Python, regex patterns are applied using functions from the re module, making text processing fast and efficient.


Common Regex Functions in Python:

Function                 Description           
re.search()Searches for a match
re.match()Matches from beginning
re.findall()Returns all matches
re.sub()Replaces text
re.split()Splits string

1️⃣ re.search() :

re.search() is used to search for a pattern anywhere in a string and returns a match object if the pattern is found.

Unlike matching from the beginning, re.search() scans the entire string to find the first occurrence of the given pattern, making it useful for checking whether specific text exists within a larger string.

Example:

import re      # Imports the re module for regular expressions text = "I love Python"     # Stores a sample text string result = re.search("Python", text)     # Searches for the word "Python" in the text if result:     # Checks if a match is found print("Match found")      # Prints message when pattern is found
Output:
Match found

"Code executed successfully" 


2️⃣ re.match() :

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

re.match() is useful when you want to ensure that a string starts with a specific pattern.
If the pattern is not found at the beginning, it returns None, even if the pattern exists later in the string.

Example:

import re # Imports the re module text = "Python is easy" # Stores the text string print(re.match("Python", text)) # Matches "Python" only at the start of the string


Output:
<re.Match object; span=(0, 6), match='Python'>


"Code executed successfully" 

This indicates that the pattern "Python" was successfully matched at the start of the string.


3️⃣ re.findall() :

re.findall() is used to find all occurrences of a pattern in a string and returns them as a list.

If the pattern appears multiple times in the text, re.findall() collects every match instead of stopping at the first one.
If no match is found, it returns an empty list, which makes it useful for counting or extracting repeated patterns.

Example:

import re # Imports the re module text = "Python Java C Python" # Stores a string with repeated words print(re.findall("Python", text)) # Finds all occurrences of "Python"

Output:
['Python', 'Python']

"Code executed successfully" 

4️⃣ re.sub() :

re.sub() is used to replace one pattern in a string with another pattern.

It searches the entire string for the given pattern and substitutes all matching occurrences with the specified replacement text, making it useful for text cleaning and modification.

Example:

import re # Imports the re module text = "I like Java" # Stores the original string print(re.sub("Java", "Python", text)) # Replaces "Java" with "Python"

Output:
I like Python

"Code executed successfully" 


5️⃣ re.split() :

re.split() is used to split a string into a list based on a specified pattern.

It works like the split() method but supports regular expression patterns, making it more powerful for complex text separation tasks.

Example:

import re # Imports the re module text = "Python,Java,C" # Stores a comma-separated string print(re.split(",", text)) # Splits the string using comma as delimiter

Output:
['Python', 'Java', 'C']

"Code executed successfully" 


Regex Special Characters:

Regex special characters are symbols that have predefined meanings and are used to build powerful search patterns.

They allow matching specific character types, positions, or repetitions in text, making regex flexible and efficient for tasks like validation, searching, and text extraction.


Symbol         Meaning    
.Any character
^Starts with
$Ends with
*0 or more
+1 or more
?0 or 1
[]Set of characters
\dDigit
\wWord character
\sWhite space

Example: Check Email Address

Regular expressions are commonly used to validate email addresses by defining a pattern that checks the correct structure (username, @, domain, and extension).

A strong regex pattern helps reduce invalid inputs and is widely used in real-life applications like login forms and registrations.

Example:

import re # Imports the regular expression module email = "test@gmail.com" # Email address to be validated # Strong regex pattern like real-life email validation pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' # ^ -> Start of string # [a-zA-Z0-9._%+-]+ -> Valid username characters # @ -> Mandatory @ symbol # [a-zA-Z0-9.-]+ -> Domain name # \. -> Dot before domain extension # [a-zA-Z]{2,} -> Domain extension (at least 2 letters) # $ -> End of string if re.match(pattern, email): # Checks if email matches the pattern print("Valid Email") # Printed if email format is correct else: print("Invalid Email") # Printed if email format is incorrect

Output:
Valid Email

"Code executed successfully" 

This pattern is strong enough for most real-world applications, though extremely
strict email validation is usually handled by backend systems or email verification services.


Example: Find Phone Number

Regular expressions are useful for extracting numeric patterns such as phone numbers from text.
By defining the exact number of digits, regex helps locate valid phone numbers within large strings, which is common in data processing and validation tasks.

Example:

import re # Imports the re module for regex operations text = "My number is 9876543210" # Text containing a phone number print(re.findall(r'\d{10}', text)) # Finds exactly 10 consecutive digits


Output:
['9876543210']

"Code executed successfully" 


🔑 Key Points:

  • \d matches any digit (0–9)

  • {10} specifies exactly 10 digits

  • re.findall() returns all matching phone numbers as a list

  • Useful for extracting phone numbers from text files, messages, or user input



Flags in Regex:

Regex flags modify how a regular expression pattern behaves during matching.
They provide additional control such as ignoring case sensitivity, handling multi-line text, and allowing the dot (.) to match newline characters, making regex more flexible for complex text processing.

Flag            Use                
re.IIgnore case
re.MMultiline
re.SDot matches newline

❌ Common Regex Mistakes :

  • Forgetting raw string r''

  • Using wrong pattern

  • Confusing search() and match()


Regex Interview Questions ?

1️⃣ Difference between search() and match()

Ans:
search() looks for a pattern anywhere in the string, while match() checks for a pattern only at the beginning of the string.


2️⃣ What is findall()?

Ans:
findall() returns a list of all occurrences of a pattern found in a string.

3️⃣ What are regex flags?

Ans:
Regex flags are special options that change how a pattern is matched, such as ignoring case, working with multiple lines, or allowing dot (.) to match newlines.



Conclusion :

Python Regular Expressions are powerful tools for text processing.
Learning regex will greatly improve your data handling skills.


💬 Quick Question

What will be the output?

import re print(re.findall(r'\d+', "Age is 20 and year is 2026"))

Comment your answer 👇😊


📌 Related Articles

Comments

popular

What Is Python Programming? Features, Uses and Career Scope.

  ✍️ Introduction Python is one of the most popular and beginner-friendly programming languages in the world today. It is widely used for web development, data science, artificial intelligence, automation, and software development. Because of its simple syntax and powerful features, Python is an excellent choice for students, beginners, and professionals. In this article, you will learn what Python programming is, its key features, real-life uses, and the career opportunities it offers. 🧠 What Is Python Programming? Python is a high-level, interpreted programming language created to make coding easy and readable. It allows developers to write programs using simple English-like statements, which makes it ideal for beginners. Python supports multiple programming styles such as procedural, object-oriented, and functional programming. ⭐ Features of Python Easy to learn and understand Simple and readable syntax Interpreted language Platform independent Large standard l...

what is Python Date and Time ? Complete Guide with Examples:

  ✍️ Introduction Python provides powerful tools to work with dates and times . The most commonly used module is datetime . The datetime module allows Python programs to create, manipulate, format, and perform calculations with dates and times easily. Date and time handling is used in: Logging systems Attendance & billing systems Data analysis Real-time applications How To Import Date and Time Module : In Python, the datetime module is used to work with dates , times , and date–time combinations . It is part of Python’s standard library, so no installation is required. You can import it in different ways depending on your need. import datetime Get Current Date and Time: This code obtains the current system date and time as a single datetime object using the datetime module. Example: import datetime now = datetime.datetime.now() print (now) Output: 2026-02-13 11:25:53.445827 "Code executed successfully"  Get Only Date: This code retrieves t...

Features of Python Programming Language Explained with Examples

✍️ Introduction Python is one of the most popular programming languages because of its powerful and easy-to-use features. It is designed to make programming simple, readable, and efficient. Python is widely used by beginners as well as professionals due to its flexibility and strong community support. In this article, we will discuss the main features of Python programming language in simple English with easy examples. 🧠 What Are Features of Python? Features of Python are the special characteristics that make it different from other programming languages. These features help programmers write clean, readable, and efficient code with less effort. ⭐ Key Features of Python Programming Language 1️⃣ Easy to Learn and Easy to Use Python has a simple syntax that is close to the English language. This makes it very easy for beginners to learn and understand programming concepts. Example: print("Hello, World!") 2️⃣ Interpreted Language Python is an interpreted language, which me...

Python Operators: Types with Examples

 ✍️Introduction In Python, operators are symbols used to perform operations on variables and values. Operators help us do calculations, comparisons, and logical decisions in programs. In this article, you will learn: • What Python operators are • Types of Python operators • Simple examples for each type