Text
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 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 whose methods skip the parser on each call. Reach for it when the same pattern runs in a loop.
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.