Mastering Regular Expressions (Regex) in Python: A Complete Guide with Cheat Sheet & Examples

Mastering Regular Expressions (Regex) in Python: A Complete Guide with Cheat Sheet & Examples

Regular Expressions, also known as regex, are a powerful tool in Python for working with strings. They allow you to search, match, and manipulate text with patterns — making them essential for tasks like data validation, parsing, and text processing.

In this comprehensive guide, we will cover everything you need to know about Regular Expressions in Python, from basics to advanced concepts, along with a complete cheat sheet and real-world examples. Let's dive in!

What is a Regular Expression?

A regular expression (regex) is a sequence of characters that defines a search pattern. You can use regex to:

  • Search for specific patterns in text.
  • Replace parts of a string.
  • Validate input data (like email, phone numbers, etc.).
  • Extract information from structured/unstructured data.

How to Use Regex in Python?

Python has a built-in module called re that provides support for regular expressions.

Importing the re Module

import re
        

Basic Regex Functions in Python

·???????? re.match(pattern, string) : Matches pattern only at the beginning of the string.

·???????? re.search(pattern, string) : Searches the string for a match (anywhere).

·???????? re.findall(pattern, string) : Returns all non-overlapping matches in a list.

·???????? re.finditer(pattern, string) : Returns an iterator yielding match objects.

·???????? re.sub(pattern, repl, string) : Substitutes occurrences of the pattern.

·???????? re.split(pattern, string) : Splits string by occurrences of pattern.

·???????? re.compile(pattern) : Compiles pattern into a regex object for reuse.

Complete Regular Expression Cheat Sheet


Sample Programs for Regular Expressions

1. Basic Match and Search

import re

text = "Hello World"

# Match
result = re.match(r"Hello", text)
print("Match:", result.group() if result else "No match")

# Search
result = re.search(r"World", text)
print("Search:", result.group() if result else "No match")
        

2. Find All Matches

text = "123 abc 456 def 789" 
# Find all digits 
result = re.findall(r'\d+', text) 
print("Digits found:", result) 

# Find all words 
result = re.findall(r'\w+', text) 
print("Words found:", result)        

3. Substitution

text = "Today is 16-03-2025" 
# Replace date format 

new_text = re.sub(r'(\d{2})-(\d{2})-(\d{4})', r'\3/\2/\1', text) 
print("Formatted Date:", new_text)        

4. Splitting Strings

text = "apple, banana; cherry orange" 
# Split on comma, semicolon, or space 

result = re.split(r'[;, ]+', text) 
print("Split List:", result)        

5. Validating Email Address

email = "[email protected]" 
# Email pattern 

pattern = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' 

if re.match(pattern, email): 
     print("Valid Email") 
else: 
     print("Invalid Email")        

6. Extracting Phone Numbers

text = "Contact us at +1-800-555-1234 or 800-555-9876" 
# Phone number pattern 
pattern = r'\+?\d{1,2}-\d{3}-\d{3}-\d{4}' 

numbers = re.findall(pattern, text) 
print("Phone Numbers:", numbers)        

7. Compiling Patterns for Reuse

pattern = re.compile(r'\d+') 
texts = ["There are 123 apples", "456 oranges", "No fruits here"] 
for t in texts: 
    print(pattern.findall(t))        

Real World Examples

? Validate IP Address

ip = "192.168.1.1" 
pattern = r'^(\d{1,3}\.){3}\d{1,3}$' 
print("Valid IP" if re.match(pattern, ip) else "Invalid IP")        


? Extract HTML Tags

html = "<div>Hello</div><p>World</p>" 
tags = re.findall(r'<.*?>', html) 
print("Tags:", tags)        

?? Tips and Tricks for Regex in Python

  • Use raw strings (r'') to avoid escaping backslashes.
  • Test patterns using online tools like regex101.
  • Use groups () to capture parts of patterns.
  • Combine multiple patterns using |.
  • Use re.VERBOSE to write readable multi-line regex.


?? Conclusion

Regular expressions in Python can be intimidating at first, but with practice, they become a powerful ally in string processing, data cleaning, and text manipulation. This guide, with its cheat sheet and examples, should give you a solid foundation to start using regex effectively in your Python projects.

Pro Tip: Start small. Build and test regex patterns incrementally to understand how they work!



要查看或添加评论,请登录

Rohit Ramteke的更多文章

  • Introduction to Large Language Models (LLMs): A Beginner's Guide

    Introduction to Large Language Models (LLMs): A Beginner's Guide

    In today's rapidly evolving world of artificial intelligence (AI), Large Language Models (LLMs) have emerged as…

    2 条评论
  • CRUD Operations using Additional Features in Flask

    CRUD Operations using Additional Features in Flask

    Introduction When building web applications, we often need to perform four essential operations: Create, Read, Update…

  • Python Coding Practices and Packaging Concepts

    Python Coding Practices and Packaging Concepts

    Introduction Writing clean, efficient, and well-structured Python code is essential for maintainability and…

  • Web Scraping: A Key Tool in Data Science

    Web Scraping: A Key Tool in Data Science

    Introduction In today’s data-driven world, information is a valuable asset. However, most of the data available on the…

    1 条评论
  • Introduction to Pandas for Data Analysis

    Introduction to Pandas for Data Analysis

    What is Pandas? Pandas is a popular open-source data manipulation and analysis library for the Python programming…

  • Introduction to NumPy

    Introduction to NumPy

    NumPy, short for Numerical Python, is a core library for numerical and scientific computing in Python. It provides…

  • Reading and Writing Files in Python

    Reading and Writing Files in Python

    Working with files is an essential part of programming. Whether you want to store data, process log files, or read…

  • Python Fundamentals with Examples

    Python Fundamentals with Examples

    Introduction Python is one of the most popular programming languages due to its simplicity, readability, and…

    1 条评论
  • Python Objects and Classes

    Python Objects and Classes

    Introduction to Classes and Objects Python is an object-oriented programming (OOP) language that revolves around…

  • Exception Handling in Python: A Comprehensive Guide

    Exception Handling in Python: A Comprehensive Guide

    Introduction In the world of programming, errors and unexpected situations are inevitable. Python, a popular and…

社区洞察