C.W.K.
Stream
Lesson 02 of 12 · published

The Dot (.) — The Universal Wildcard

~8 min · dot, wildcard, dotall

Level 0Pattern-Curious
0 XP0/90 lessons0/15 achievements
0/100 XP to next level100 XP to go0% complete

One pattern symbol, almost any character

. matches any single character except a newline. That "except newline" is the part everyone forgets and that bites everyone in production.

Pattern c.t matches cat, cot, cut, c@t, c1t, c t, c.t (yes, the literal dot too — because . matches any character including a literal dot).

Pattern . by itself matches every character one at a time, except newlines. Run re.findall(r'.', 'abc') in Python and you get ['a', 'b', 'c'].

The DOTALL / 's' flag

If you want . to also match newlines, you need the DOTALL flag. Python: re.DOTALL or re.S. PCRE / JavaScript / many others: s flag, or the inline (?s). Without it, your pattern stops at every \n in multi-line input — a classic source of "my regex works on one line but not two."

The dot is greedy company

Combined with quantifiers (Track 3), .* means "any character, zero or more times." This is the most powerful and most dangerous combination in all of regex. .* will happily eat your entire file before backtracking. Use lazy quantifiers (.*?) or character classes ([^"]*) instead, almost always. We'll devote multiple lessons to this in the Quantifiers track.

Code

The dot's quirks·python
import re

# Dot matches anything... except newline
re.findall(r'.', 'a\nb\nc')
# ['a', 'b', 'c']  — newlines skipped

# DOTALL flag makes dot match newlines too
re.findall(r'.', 'a\nb\nc', re.DOTALL)
# ['a', '\n', 'b', '\n', 'c']

# Inline DOTALL
re.findall(r'(?s).', 'a\nb')
# ['a', '\n', 'b']

# Literal dot — escape it
re.findall(r'\.', 'version 3.14')
# ['.']

External links

Exercise

Try matching <p>Hello\nWorld</p> with the pattern <p>(.+)</p>. Does it match? Now turn on DOTALL. Compare. Then try the portable alternative <p>([\s\S]+)</p> without DOTALL. All three behaviors should now make sense.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.