Why can a regex dot fail to match a line break?
Quick answer
A regex dot usually matches one character but excludes line breaks by default. In Python, use re.DOTALL (or re.S); in JavaScript, add the s flag, as in /a.b/s. These settings let the dot match line terminators. The default exclusions differ between the languages, so check the behavior for your input and regex engine.
The question, the details, the next step.
A practical checklist
Identify the language and regex engine that run the pattern.
Test the smallest failing example, including the actual line break.
For Python
re, tryre.DOTALLorre.S; use(?s:...)when only one group needs dot-all.For JavaScript, add the
sflag to the regex literal or corresponding API option.Test CRLF or other relevant terminators separately if they occur in your input.
A few more useful answers
Does Python’s `re.MULTILINE` make dot match a newline?
No. In Python, re.MULTILINE affects ^ and $; use re.DOTALL or re.S for dot-all behavior. [S1]
Can I enable dot-all for only part of a Python pattern?
Yes. Python supports the scoped form (?s:...), which enables dot-all within that group. [S1]
Does JavaScript dot-all change how dot handles line terminators?
The cited JavaScript reference says the s flag lets . match line terminators, including LF, CR, U+2028, and U+2029. [S2]
Sources
References used for this guide.
- [S1] Python 3.14.7: Regular expression operations docs.python.org · Checked
- [S2] MDN: JavaScript regular-expression character classes developer.mozilla.org · Checked
