Regular expressions and text guides
Multiline vs dotAll: anchors and newlines are separate
Understand why m changes anchors while s changes the dot, and build predictable multiline extraction patterns.
Multiline changes anchors
The m flag makes ^ and $ refer to line boundaries. It does not make a dot match a newline.
For a log with separate lines, use anchored patterns to identify each line. Add g if you need every matching line rather than the first.
Input: ok
ERROR failed
Pattern: ^ERROR.*$
Flags: gm
Match: ERROR failedDotAll changes the dot
The s flag lets . match line terminators. Anchors still refer to the whole input unless m is also enabled.
For text between known delimiters, dotAll can span lines. Confirm that the delimiters are literal text and are not nested structural syntax.
Input: BEGIN
first
second
END
Pattern: BEGIN.*?END
Flags: sAccount for line endings
Files may use LF or CRLF. Captured line contents can include a carriage return if the pattern does not account for it.
Inspect invisible characters before adding broad trim calls. Trimming all whitespace can remove indentation or significant trailing spaces.
Use structured parsers for nesting
A lazy dot changes how much text is consumed; it does not teach regex to parse arbitrary nested HTML, JSON or balanced containers.
Test missing and repeated delimiters. For a structured source, use its parser and transform the selected field instead of relying on a broad multiline match.