Regular Expressions Cookbook, 2nd Edition
by Steven Levithan
Published by
O'Reilly Media, Inc., 2012
and
Tags
| Regex options: None |
| Regex flavors: .NET, Java, XRegExp, PCRE, Perl, Python |
(?m)'.'
| Regex options: None |
| Regex flavors: Ruby |
If you cannot turn on “dot matches line breaks” mode outside the regular expression, you can place a mode modifier at the start of the regular expression. We explain the concept of mode modifiers, and JavaScript’s lack of support for them, in the subsection Case-insensitive matching in Recipe 2.1.
‹(?s)› is
the mode modifier for “dot matches line breaks” mode in .NET, Java,
XRegExp, PCRE, Perl, and Python. The s stands for “single line” mode, which is
Perl’s confusing name for “dot matches line breaks.”
The terminology is so confusing that the developer of Ruby’s regex
engine copied it wrongly. Ruby uses ‹(?m)› to
turn on “dot matches line breaks” mode. Other than the different letter,
the functionality is exactly the same. The new engine in Ruby 1.9
continues to use ‹(?m)› for
“dot matches line breaks.” Perl’s very different meaning for ‹(?m)› is
explained in Recipe 2.5.
In many cases, you don’t want to match truly any character, but rather any character except a select few. Recipe 2.3 explains how to do that.
Recipe 3.4 explains how to set options such as “dot matches line breaks” in your source code.
When working with Unicode text, you may prefer to use ‹\X› to
match a Unicode grapheme instead of the dot which matches a Unicode code
point. Recipe 2.7 explains this in
detail.