Text

Regular Expressions

The re module searches and extracts text using regular expressions.

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
^ and $ anchor the pattern; quantifiers like {2} bound how many times a token repeats.

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.