Regular Expressions

A regular expression (regex) is a mini pattern-matching language for text: instead of describing an exact string, you describe a shape that a string can match — "a digit, then three letters, then another digit", for example. Python's built-in re module gives you regex support out of the box.

The re Module

Regular expression support lives in the standard library's re module, so every regex-based script starts with:

import re

Commonly Used Functions

Most regex work in Python goes through a small handful of functions from the re module:

FunctionWhat it does
re.match(pattern, text)Checks for a match only at the beginning of the string.
re.search(pattern, text)Scans the whole string and returns the first match found anywhere.
re.findall(pattern, text)Returns a list of all non-overlapping matches in the string.
re.sub(pattern, repl, text)Returns a new string with every match replaced by repl.
text = "Order 12 and order 45 shipped"

print(re.match(r"Order", text))    # matches, string starts with "Order"
print(re.search(r"\d+", text))    # finds "12", the first match anywhere
print(re.findall(r"\d+", text))   # ['12', '45'], every match

Basic Pattern Syntax

A regex pattern is built from literal characters mixed with special symbols that stand for a class or a quantity of characters:

SymbolMeaning
\dAny single digit (0-9)
\wAny word character (letters, digits, underscore)
\sAny whitespace character (space, tab, newline)
+One or more of the preceding item
*Zero or more of the preceding item
.Any single character (except a newline)

A Simple Email-Validation Example

Combining these symbols lets you check whether a string roughly looks like an email address. The pattern below is deliberately simple — it is not a fully RFC-compliant email validator, but it catches the common shape of "some characters, an @, more characters, a dot, a few letters":

pattern = r"^\w+@\w+\.\w+$"

def looks_like_email(value):
    return re.match(pattern, value) is not None

print(looks_like_email("sonu@example.com"))  # True
print(looks_like_email("not-an-email"))      # False

Note: The ^ anchors the pattern to the start of the string and $ anchors it to the end, so the entire string must match the shape, not just part of it.

Replacing Text with re.sub()

re.sub() searches for every match of a pattern and replaces it with a given string, which is useful for cleaning up or redacting text:

text = "Call 9876543210 or 8123456789"
masked = re.sub(r"\d+", "[number]", text)
print(masked)  # Call [number] or [number]

Why Regex Patterns Use Raw Strings

Regex patterns are almost always written as raw strings, using an r prefix like r"\d+". In a normal Python string, a backslash starts an escape sequence (\n for newline, \t for tab), so Python's own string parser would try to interpret \d before the re module ever saw it. A raw string tells Python to leave every backslash exactly as typed, which avoids that conflict and hands the regex engine the pattern you actually intended.

# without r-prefix, \d isn't a recognized Python escape, but it's easy to get this wrong
pattern_confusing = "\\d+"   # requires doubling the backslash

# with r-prefix, the backslash is passed through as-is
pattern_clear = r"\d+"       # clean and readable
Next steps: Head to Best Practices to wrap up the Core Python track with a checklist of good habits.