Alright, that title doesn't really work, but one thing I've encountered quite frequently is that the terms "single-line mode" and "multi-line mode" seem to cause no end of confusion for the vast majority of regex users. Many guides try to explain the terms based on some description of lines, or other unrelated issues. I won't, because I think that's highly misleading. In fact, I shunned the terms in RegexPal in favor of the hopefully more descriptive "dot matches all" (instead of single-line) and "^$ match at line breaks" (instead of multi-line). Another sensible name I've seen used for multi-line mode is "enhanced line-anchor mode".
The first important thing to understand is that single-line and multi-line modes have nothing to do with each other. Hence, they can be applied independently — e.g., sometimes it makes sense to use both at the same time. Single-line mode changes the meaning of the dot, while multi-line mode changes the meaning of ^
and $
. There are no other side effects. This means that if your regex does not contain one of those three metacharacters (.
, ^
, $
), neither modifier has any impact (except possibly confusing other people who read your regexes).
Let's get more specific…
- Dot (
.
) matches all characters except newlines. In single-line mode, it matches all characters. - Caret (
^
) matches the position at the beginning of the string. In multi-line mode, it additionally matches the position just after newlines. - Dollar (
$
) matches the position at the end of the string. With most regex flavors, it also matches just before a string-ending newline. In multi-line mode, it additionally matches the position just before newlines (not only string-ending newlines).
As for exactly which characters are considered newline characters, that is regex-flavor and character-encoding specific. See my last post, JavaScript, Regex, and Unicode, for a precise JavaScript definition.
Finally, although single-line and multi-line modifiers are standard in most Perl-derivative regex flavors, there are a couple notable exceptions.
- JavaScript does not have a single-line modifier. Use
[\S\s]
instead of a dot if you want to match any character including newlines. And for the love of god, don't use(.|\r|\n)
or similar — it's terribly inefficient (especially when repeated infinitely) and doesn't match a couple lesser-used newline characters. - In JavaScript,
$
without/m
matches only the position at the end of the string. It does not match before a string-ending newline. - In Ruby,
^
and$
always match at newlines, and there is no mode to change this. Use\A
and\Z
to match at the beginning and end of the string only. In fact, what Ruby calls multi-line mode (and implements as/m
) is what other regex flavors call single-line mode! Talk about confusing!
Edit: On his blog, Jeffrey Friedl pointed out a related post he'd made on the comp.lang.perl Usenet group back in 1994.
Edit 2: Note that I consider Tcl's "Advanced Regular Expressions" flavor to be more inspired by some features of Perl than actually Perl-derivative. Hence, its peculiarities regarding so-called single-line and multi-line handling are not mentioned here.