Python

Regular Expressions

Regular Expressions (Regex or Regexp) are extremely powerful sequences of characters that define a search pattern. They are used to search, extract, or replace specific patterns of text inside strings (like finding all email addresses in a document or validating a password).

In Python, you use the built-in re module to work with regular expressions.

Basic Concepts

  • Pattern: The regex formula you write to find a match.
  • Raw Strings (r"..."): Always use raw strings for regex in Python. It tells Python not to treat backslashes as escape characters (e.g., \n stays as \n instead of becoming a new line).

1. Import the re Module

import re

2. Find the First Match (re.search)

re.search() scans the entire string and returns a "Match object" for the first location where the pattern is found.

import re

text = "My phone number is 9841000000. Call me!"
# \d means "digit", + means "one or more"
pattern = r"\d+" 

match = re.search(pattern, text)

if match:
    print("Found:", match.group()) # .group() extracts the matched text

Output:

Found: 9841000000

3. Find Matches ONLY at the Beginning (re.match)

re.match() checks for a match only at the very start of the string.

import re

text = "Python is fun"

# Trying to match "Python"
match1 = re.match(r"Python", text)
print(bool(match1))

# Trying to match "fun" (fails because it's not at the start)
match2 = re.match(r"fun", text)
print(bool(match2))

Output:

True
False

4. Find All Matches (re.findall)

This is the most commonly used function. It returns a list of all non-overlapping matches in the string.

import re

text = "There are 3 apples, 14 oranges, and 250 bananas."
pattern = r"\d+" # Find all numbers

numbers = re.findall(pattern, text)
print(numbers)

Output:

['3', '14', '250']

5. Search and Replace (re.sub)

re.sub() (substitute) replaces the matches with a new string.

import re

text = "I have a cat. My cat is black."
pattern = r"cat"
replacement = "dog"

new_text = re.sub(pattern, replacement, text)
print(new_text)

Output:

I have a dog. My dog is black.

6. Split a String by a Pattern (re.split)

Similar to the standard .split() method, but you can split by complex patterns (like multiple types of punctuation).

import re

# We want to split by commas, semicolons, or spaces
text = "apple,banana;orange grape"
pattern = r"[,\s;]+" 

fruits = re.split(pattern, text)
print(fruits)

Output:

['apple', 'banana', 'orange', 'grape']

7. Custom Character Sets [...]

You can define your own rules by putting characters inside square brackets.

import re

text = "bat, cat, rat, mat, hat"
pattern = r"[bc]at" # Match only "bat" or "cat"

print(re.findall(pattern, text))

Output:

['bat', 'cat']

8. Anchors: Start ^ and End $

Anchors don't match characters; they match positions (the beginning or end of a string).

import re

text = "Hello World"

# Does the string START with "Hello"?
print(bool(re.search(r"^Hello", text)))

# Does the string END with "World"?
print(bool(re.search(r"World$", text)))

Output:

True
True

9. Grouping (...)

Parentheses allow you to group parts of a pattern together so you can extract specific pieces.

import re

# Let's extract the domain name from an email
email = "ram@gmail.com"
# \w+ matches word characters, @ is literal, (\w+) captures the domain, \. is literal dot
pattern = r"\w+@(\w+)\.com" 

match = re.search(pattern, email)
if match:
    print("Full match:", match.group(0))
    print("Extracted Group 1:", match.group(1))

Output:

Plaintext

Full match: ram@gmail.com
Extracted Group 1: gmail

10. Practical Example: Validating an Email

A simple regex to check if a string looks like an email address.

import re

def is_valid_email(email):
    # Start -> characters -> @ -> characters -> . -> 2 to 4 characters -> End
    pattern = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-]{2,4}$"
    return bool(re.search(pattern, email))

print(is_valid_email("test@example.com"))
print(is_valid_email("invalid-email.com"))

Output:

True
False

Regex Cheatsheet

Character Classes (Shortcuts)

PatternMeaningExample Match
\dAny digit (0-9)5
\DAny NON-digitA, !, space
\wAny word character (a-z, A-Z, 0-9, _)a, 7, _
\WAny NON-word character!, @, space
\sAny whitespace (space, tab, newline) 
.Any character EXCEPT newlinex, 9, %

Quantifiers (How many times?)

PatternMeaning
*0 or more times
+1 or more times
?0 or 1 time (optional)
{3}Exactly 3 times
{2,5}Between 2 and 5 times

 

Interactive Sandbox
Python