Text
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 9re.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
9For a simple substring check, ordinary string membership is clearer than regex.
Source
print("Grace" in text)Output
Truere.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
Nonere.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
- Use raw strings for regex patterns so backslashes are easier to read.
- Use capturing groups when the point is extraction, not just matching.
re.matchanchors at the start;re.searchfinds the first match anywhere.re.compilesaves work when the pattern runs more than once.re.subrewrites matches; flags likere.IGNORECASEchange matching behavior without rewriting the pattern.- Reach for string methods before regex when the pattern is simple.
See also
- related: Strings
- related: String Formatting
Run the complete example
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.