Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

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.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

Why does a.b match a-b but not a\nb? I expected the dot to stand for anything.

Geez

The dot usually means “match one character,” but many regex engines exclude line breaks from that rule by default. So a.b can match the hyphen in a-b, while it does not cross the newline in a\nb. The dot does not mean “match any length of text,” either: it matches a single character when it does match.

To let the dot match line breaks, enable the engine’s dot-all mode. The setting is language-specific, and it changes what . matches; it does not remove line breaks from the text.

Follow-up question

How can I check the difference in Python?

Geez

In Python’s re module, the documented default is that . matches any character except a newline. re.DOTALL (also written re.S) lets it match newline characters; the scoped form (?s:.) applies dot-all just to a group. These details are from the Python 3.14.7 reference. [S1]

Here is a small test using the same pattern and text in both searches:

import re

text = "a\nb"
pattern = r"a.b"

print(bool(re.search(pattern, text)))            # False
print(bool(re.search(pattern, text, re.DOTALL))) # True

The first search uses the default behavior. The second enables dot-all for that search. If only one part of a larger pattern needs dot-all, a scoped group such as (?s:.) applies it just there.

Follow-up question

What’s the equivalent in JavaScript, and does it treat line endings exactly the same?

Geez

In JavaScript, add the s flag to the regular-expression literal. The same pattern and text can be checked like this:

const text = "a\nb";

console.log(/a.b/.test(text));  // false
console.log(/a.b/s.test(text)); // true

JavaScript’s default dot excludes four line terminators: LF (\n), CR (\r), U+2028, and U+2029. With the s flag, dot also matches line terminators. [S2]

That differs in how the defaults are documented: the cited Python reference describes excluding a newline, while the JavaScript reference explicitly lists four line terminators. The examples above demonstrate LF; they do not test every terminator. If your input uses CRLF (\r\n) or another line terminator, test that exact text too.

Follow-up question

Could another flag be why my dot still fails? What should I check before changing the pattern?

Geez

First check which language and regex API are actually running the pattern, then confirm that dot-all is enabled using that API’s syntax. In Python, re.MULTILINE changes how ^ and $ behave; it does not make . match newline. [S1] In JavaScript, the dot-all setting is the s flag. [S2]

Try the smallest failing case, such as a\nb, before applying the setting to a larger expression. If needed, repeat the test with the exact line-ending characters in your input. The cited references describe Python re and JavaScript; they do not establish how a third-party engine, wrapper, or editor handles its own options.

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, try re.DOTALL or re.S; use (?s:...) when only one group needs dot-all.

  • For JavaScript, add the s flag 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.

  1. [S1] Python 3.14.7: Regular expression operations
    docs.python.org · Checked
  2. [S2] MDN: JavaScript regular-expression character classes
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work