Python Regular Expressions (Regex)

Regular expressions (regex) are sequences of characters that define a search pattern. They are primarily used for string matching and manipulation in Python. Python has a built-in package called re for working with regular expressions.

What is a Regular Expression?

A regular expression is a special string used to match patterns in text. It's used for tasks such as searching, replacing, and validating string patterns. Regex is widely used in text processing, web scraping, and data validation.

Syntax of Regular Expressions

Regular expressions follow a set of syntactical rules. Some common regex metacharacters are:

Using Regular Expressions in Python

Python provides the re module to work with regular expressions. Some common functions provided by the re module include:

Examples

1. Matching a Pattern


import re

# Check if the pattern matches the start of the string
pattern = "^Hello"
string = "Hello, World!"

match = re.match(pattern, string)
if match:
    print("Match found!")
else:
    print("Match not found!")
            

Output

Match found!

2. Finding All Occurrences of a Pattern


import re

# Find all occurrences of the pattern 'o' in the string
pattern = "o"
string = "Hello, World!"

matches = re.findall(pattern, string)
print(matches)
            

Output

['o', 'o']

3. Replacing a Pattern in a String


import re

# Replace 'World' with 'Python' in the string
pattern = "World"
string = "Hello, World!"

new_string = re.sub(pattern, "Python", string)
print(new_string)
            

Output

Hello, Python!

4. Splitting a String Using a Pattern


import re

# Split the string at each occurrence of the pattern 'o'
pattern = "o"
string = "Hello, World!"

result = re.split(pattern, string)
print(result)
            

Output

['Hell', ', W', 'rld!']

Python’s regular expressions allow you to perform a wide range of string matching and manipulation tasks efficiently. Understanding the various functions and how to apply regex patterns will make it easier to work with complex strings in your Python projects.