XML technologies / Regular expressions / Features and dialects / Strings, character encodings, and modes / regex modes
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
Most regex engines can operate in several modes, depending on how a regular expression should be interpreted or applied. We have already seen examples: the free-spacing mode in Perl with the /x modifier (the regex mode allowing whitespace and comments in the regex; see Section on the x modifier) concerns interpretation, whereas case-insensitive mode with the /i modifier concerns how the regex is applied (see Section on the i modifier).
Modes of this kind usually apply to the entire regex, though newer dialects also allow them for individual subexpressions. A mode affecting the whole regex is generally selected with a modifier or option, such as /i in Perl, the i modifier in PHP (see Table: pattern modifiers in PHP), or Pattern.CASE_INSENSITIVE in Java (see Section on searching and replacing in Java). Where supported, notation such as ˹(?i)˼ enables the mode inside the regex, and ˹(?-i)˼ disables it again. Some dialects also support ˹(?i:...)˼ and ˹(?-i:...)˼, enabling or disabling case-insensitive mode only for the parenthesized subexpression.
You will learn exactly how to use these modes in a regex later (see the section on mode modifiers). Here we briefly introduce only the common modes.
Case-insensitive mode
This mode exists in virtually every language and tool. Uppercase and lowercase letters are considered equivalent, so ˹b˼ matches both ›b‹ and ›B‹. Naturally, the character encoding must be configured correctly.
Surprisingly, this mode has repeatedly suffered from bugs. In Ruby, for example, it does not work when a letter is specified as an octal or hexadecimal escape.
Further problems arise with Unicode, where this mode is called “loose matching”. First, not every alphabet has uppercase and lowercase letters; others also have titlecase letters, a form appearing only at the beginning of a word and not always identical to ordinary uppercase. Nor is the correspondence between uppercase and lowercase always unambiguous in both directions. For example, Greek sigma (Σ) has two lowercase forms, σ and ς; all three should match when case is ignored. Of the systems I tested, only Perl and java.util.regex get this right.
One character may also become two: the classic case is “ß” and its uppercase rendering “SS”. Only Perl handles this correctly.
Some Unicode problems are self-inflicted. For example, there is a code point for ǰ (U+01F0), but its uppercase counterpart, ˇJ, can only be represented using a combining character, U+004A and U+030C (see the section Characters and character combinations). Nevertheless, ǰ and ˇJ should be equivalent in case-insensitive mode. There are even examples of a lowercase letter represented by a single code point whose uppercase counterpart requires a sequence of three. Fortunately, these affect only rare characters.
Free-spacing mode
In this mode, whitespace outside character classes does not count (the java.util.regex package also ignores whitespace inside character classes). Everything from a # to the end of the line is also treated as a comment. You have already seen examples in Perl (see the section x modifier), Java (see the section Searching and replacing in Java), and VB.NET (see the section Searching and replacing in VB.NET).
Actually, only in java.util.regex is whitespace completely ignored. Other implementations instead treat whitespace as a kind of no-op metacharacter. This makes a difference with ˹\12●3˼: in this mode, it is interpreted as ˹\12˼ followed by ˹3˼, rather than ˹\123˼ as one might expect.
Exactly what counts as whitespace naturally depends on the character encoding and how the language implements it. Usually, only ASCII whitespace characters are recognized.
Dot-matches-all mode
Normally, dot does not match a newline. The original Unix regex programs processed text line by line, so the question did not arise until sed and lex appeared. By then, the subexpression ˹.*˼ had informally become established as “up to the end of the line”, so including newline was considered “too unwieldy” (note: the words of Ken Thompson, author of ed). Thus, even in programs handling multiline data, such as editors, dot normally does not match the line ending.
In a programming language, however, a dot that really matches every character can be just as useful. Which behavior is best depends on the situation. Many languages therefore let you select the appropriate mode for each regex.
There are exceptions. In Unicode-aware systems such as Sun’s Java package, dot normally matches none of the Unicode line-ending characters represented by a single code point (see the section Line endings in Unicode). In Tcl, dot normally matches every character, including line endings; in newline mode and partial newline mode, neither dot nor a negated character class matches newline.
An unfortunate name
When the /s modifier first appeared in Perl, it was called “single-line mode”. This unfortunate name still haunts the poor modifier, although it really has nothing to do with ˹^˼ or ˹$˼; these and multiline mode are discussed next.
Enhanced line-anchor mode, or multiline mode
In multiline mode, the line anchors ˹^˼ and ˹$˼ acquire slightly different meanings. Normally, the anchor ˹^˼ matches only at the very beginning of the subject string, not at the beginning of logical lines within it when it contains newlines. In multiline mode, ˹^˼ also matches the beginning of those logical lines: the string is correctly recognized as consisting of multiple lines. You encountered this mode in Extended introductory examples, when we developed a program to convert plain text to HTML (see Converting plain text to HTML). The entire text file was read into a single string, and we used the substitution s/^$/<p>/mg to turn blank lines such as “...Kapitel.NL NLWenn...” into paragraph tags such as “...Kapitel.NL<p>NLWenn...”.
With ˹$˼ the situation is analogous, although the rules governing what dollar normally matches are somewhat more complicated (see the section End of line, end of string: $, \Z, \z). For now, it suffices to say that in multiline mode ˹$˼ can also match immediately before a newline in the middle of the string.
Programs supporting multiline mode often also provide the metacharacters ˹\A˼ and ˹\Z˼. They normally behave like ˹^˼ and ˹$˼, retaining that behavior even in multiline mode. In other words, ˹\A˼ always matches only the beginning of the string, even in multiline mode. Some implementations also let ˹$˼ or ˹\Z˼ match just before the newline at the end of the subject string. Such implementations often offer the additional metacharacter ˹\z˼, which ignores newlines entirely and matches only the true end of the subject string. The section End of line, end of string: $, \Z, \z gives all the details.
As with dot, some implementations differ. In an editor such as GNU Emacs, line anchors normally match newlines within the search text, surely the most sensible behavior for an editor. In lex , however, ˹$˼ matches only before a newline (˹^˼ behaves normally here).
In Unicode-aware systems such as Sun’s java.util.regex, line anchors in multiline mode can match before or after any of Unicode’s various line-ending characters (see the section Line endings in Unicode). In Ruby, multiline mode is the default, and ˹\Z˼ in Python behaves like the ˹\z˼ introduced in the preceding paragraph, rather than like ˹$˼ as in most programming languages.
The name “multiline mode” may suggest a relationship with the unfortunate term “single-line mode”, which should be avoided. There is none. One mode changes dot’s behavior; the other changes ˹^˼ and ˹$˼. Both concern newline characters, but from different perspectives. The first changes newline from “special” to “ordinary” for dot; multiline mode changes how ˹^˼ and ˹$˼ treat newlines from “ordinary” to “special”. (Note: in Tcl, dot normally matches every character, and neither dot nor line anchors treat newline specially. Options can change this behavior. This is actually much clearer than in other languages, but since the other tools and languages have always behaved differently, we treat Tcl’s more logical behavior as the exception.)
Literal-text mode
In literal-text mode, all or nearly all regex metacharacters become ordinary characters. For example, ˹[a-z]*˼ matches the string ›[a-z]*‹. Matching in literal-text mode is thus a simple substring search: “find this string” rather than “find a match for this regex”. Most programs already offer a search function entirely unrelated to regular expressions for this. It becomes interesting when literal-text mode can be selectively enabled or disabled for parts of a regex or subexpressions. PCRE (and therefore PHP) and Perl allow this with the sequence \Q...\E: between ˹\Q˼ and ˹\E˼, every metacharacter becomes an ordinary, literal character.
German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln