re Anchors Respect MULTILINE And DOTALL
By default ^ and $ match whole-string ends only; MULTILINE moves them to every line break while DOTALL lets a dot cross newlines to span several lines.
Code
import re
log = """INFO boot complete
ERROR disk full on /var
INFO retry scheduled
ERROR backup aborted"""
# Without flags, ^ and $ mean whole-string ends only: no match.
print("no flag :", re.findall(r"^ERROR.*$", log))
# MULTILINE makes ^ and $ match at every line break.
print("MULTILINE:", re.findall(r"^ERROR.*$", log, flags=re.MULTILINE))
# DOTALL lets . cross newlines, so one pattern can span several lines.
record = "id=7\nstatus=ok\nchecksum=abc"
print("DOTALL :", re.search(r"id=\d+.*checksum=\w+", record, flags=re.DOTALL).group(0))Output
Captured from a sandboxed run of this snippet in the build pipeline.
no flag : []
MULTILINE: ['ERROR disk full on /var', 'ERROR backup aborted']
DOTALL : id=7status=okchecksum=abcHow the snippet works
Everything operates on top-level bindings: log, record.
What to watch for
- Self-contained — no imports beyond what the runtime already provides.
Using it
Running the file prints labeled results such as no flag :, MULTILINE:, DOTALL : — each operation’s effect is visible in the output.
Interactive Tools for This Topic
Regular Expression TesterTest regular expressions against sample text with live match highlighting, capture group extraction, and flags support for global, case-insensitive, and multiline modes.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.Nginx Location Block TesterTest any URL path against Nginx location blocks using authentic Nginx precedence (= exact, ^~ prefix, regex ~ and ~*, and longest prefix) with trace output.Regex Backreference ExplainerScan a regular expression to list its capturing groups, named groups, non-capturing groups, and backreferences, with warnings for dangling references.
FAQ
- Are there any imports to resolve outside the language?
- No. Every import resolves against the language's own modules. Its state lives in module-level bindings: `log`, `record`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.