Hone

Lessons · Regex · the dot stops at a newline

The dot stops at a newline

The dot means any character except a newline, unless you turn on DOTALL.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

It is the one exception to 'any character', and it exists so a pattern written for one line does not run off the end of it.

How to think about it

If the match is meant to span lines, either turn on re.S or write the class that means everything, [\s\S], where you need it.

Worked example

re.search(r'a.b', 'a\nb')
The dot will not cross the newline.
re.search(r'a.b', 'a\nb', re.S).group()
With DOTALL it will.
re.search(r'a[\s\S]b', 'a\nb').group()
Whitespace or not-whitespace is every character, and it needs no flag.

Your turn

Let the dot match across a newline.

re.search(r'a.b', 'a\nb', re.).group()

The trap

A flag applies to the WHOLE pattern. [\s\S] applies exactly where you put it, which is usually what you meant.

Practise the dot stops at a newline on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.