Text

Regular Expressions

The re module searches and extracts text using regular expressions.

Regular expressions are a compact language for searching and extracting text patterns. Python's re module provides the standard interface: re.match anchors at the start of the string, re.search finds the first occurrence anywhere, re.findall collects every match, re.sub rewrites matches, and re.compile reuses a pattern.

Use regex when the pattern has structure: repeated records, alternatives, optional parts, or pieces you want to capture. Prefer ordinary string methods for simple substring checks because simpler code is easier to maintain.

Flags such as re.IGNORECASE adjust matching behavior without rewriting the pattern. Pair them with re.compile when the same pattern is used repeatedly.

Raw strings keep backslashes readable in regex patterns. Capturing groups return just the pieces inside parentheses.

Source

import re

text = "Ada: 10, Grace: 9"
pattern = r"([A-Za-z]+): (\d+)"

for name, score in re.findall(pattern, text):
    print(name, int(score))

Output

Ada 10
Grace 9
findall returns only what the parentheses capture: ([A-Za-z]+) takes the name and (\d+) the score, one (name, score) pair per record.

re.search() finds the first match. A match object exposes captured groups by position.

Source

match = re.search(r"Grace: (\d+)", text)
print(match.group(1))

Output

9

For a simple substring check, ordinary string membership is clearer than regex.

Source

print("Grace" in text)

Output

True

re.match only matches at the start of the string; re.search finds the first match anywhere. Picking the right one keeps anchoring intent visible without an explicit ^.

Source

start = re.match(r"Ada", text)
print(start is not None)
print(re.match(r"Grace", text))

Output

True
None

re.compile produces a reusable pattern object and gives the pattern a name. The re module also caches recently compiled patterns internally, so the practical wins are readability and a place to attach flags more than raw speed.

Source

scoreline = re.compile(pattern)
print(scoreline.findall(text))

Output

[('Ada', '10'), ('Grace', '9')]

Flags such as re.IGNORECASE adjust matching without changing the pattern. re.sub replaces every match with a replacement string and returns the rewritten text.

Source

casey = "ADA: 11"
print(re.search(r"ada", casey, flags=re.IGNORECASE).group(0))

print(re.sub(r"\d+", "?", text))

Output

ADA
Ada: ?, Grace: ?

Notes

See also

Run the complete example

Example code

Expected output

Ada 10
Grace 9
9
True
True
None
[('Ada', '10'), ('Grace', '9')]
ADA
Ada: ?, Grace: ?

Execution time appears here after you run the example.