Sometimes you don't care where a pattern appears — you just want to know if it's there. in handles literal substrings. What handles patterns like "any 3-digit error code"?
Regex. Something like \d{3} would match any three digits. But I've only ever seen it in tutorials — I don't know which function to call.
re.search(pattern, string) scans the whole string looking for the pattern. It returns a Match object if it finds one, or None if it doesn't. That truthiness makes it perfect for if checks:
import re
if re.search(r"\b404\b", line):
...The \b marks a word boundary — between a digit/letter and a non-digit/non-letter — so 404 doesn't match inside 40412.
Why re.search and not re.match? The names sound like they do the same thing.
They don't. re.match only checks the start of the string. re.search scans the whole string. Nine times out of ten, you want search — match is specialized for "is the whole string this shape?" cases.
And what makes the r"" prefix different from a normal string?
r"" is a raw string — backslashes stay as backslashes. Without the r, Python would interpret \b as a backspace character before regex ever sees it. Every regex pattern lives inside r"". Make it a habit:
import re
pattern = r"\bERROR\b"
if re.search(pattern, line):
return True
return FalseSo re.search is the regex version of in — yes or no, pattern is in the string.
Good analogy. re.search is for presence checks — yes-or-no answers about whether a pattern exists inside a string. A clean, reliable test.
re.search + \bTL;DR: re.search(pattern, string) returns a truthy match or None — use it like a pattern-aware in.
re.search — scans anywhere in the stringre.match — only checks from position 0\b — word boundary, prevents partial matchesr"..." — raw string, keeps backslashes literal| Need | Function |
|---|---|
| substring check | in |
| pattern anywhere | re.search |
| pattern at start | re.match |
Write `has_error_code(line, code)` that returns `True` if the string `code` appears in `line` as a whole word (not inside a longer number). Use `re.search` with `\b` word boundaries on both sides of the code.
Tap each step for scaffolded hints.
No blank-editor panic.
Sometimes you don't care where a pattern appears — you just want to know if it's there. in handles literal substrings. What handles patterns like "any 3-digit error code"?
Regex. Something like \d{3} would match any three digits. But I've only ever seen it in tutorials — I don't know which function to call.
re.search(pattern, string) scans the whole string looking for the pattern. It returns a Match object if it finds one, or None if it doesn't. That truthiness makes it perfect for if checks:
import re
if re.search(r"\b404\b", line):
...The \b marks a word boundary — between a digit/letter and a non-digit/non-letter — so 404 doesn't match inside 40412.
Why re.search and not re.match? The names sound like they do the same thing.
They don't. re.match only checks the start of the string. re.search scans the whole string. Nine times out of ten, you want search — match is specialized for "is the whole string this shape?" cases.
And what makes the r"" prefix different from a normal string?
r"" is a raw string — backslashes stay as backslashes. Without the r, Python would interpret \b as a backspace character before regex ever sees it. Every regex pattern lives inside r"". Make it a habit:
import re
pattern = r"\bERROR\b"
if re.search(pattern, line):
return True
return FalseSo re.search is the regex version of in — yes or no, pattern is in the string.
Good analogy. re.search is for presence checks — yes-or-no answers about whether a pattern exists inside a string. A clean, reliable test.
re.search + \bTL;DR: re.search(pattern, string) returns a truthy match or None — use it like a pattern-aware in.
re.search — scans anywhere in the stringre.match — only checks from position 0\b — word boundary, prevents partial matchesr"..." — raw string, keeps backslashes literal| Need | Function |
|---|---|
| substring check | in |
| pattern anywhere | re.search |
| pattern at start | re.match |