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.

PythonData Integrity

Code

pythonsnippet
16 lines
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=abc

How 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

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`.