XML technologies / Regular expressions / Features and dialects / Common metacharacters / Comments and mode⟦U+0002⟧ modifiers
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
In many dialects, the following constructs let you set or change the regex mode (see Regex modes) within the regex, on the fly.
Mode modifiers: (?Modifikator), e.g. (?i) and (?-i)
Many implementations allow regex modes to be controlled within the expression. They often support the special notation ˹(?i)˼ for case-insensitive matching, and ˹(?-i)˼ to turn that behavior off. For example, in ˹<B>(?i)sehr(?-i</B>)˼, the ˹sehr˼ part matches regardless of case, while tag names remain case-sensitive. Thus, ›<B>SEHR</B>‹ and ›<B>Sehr</B>‹ would match, but ›<b>Sehr</b>‹ would not.
This example works in most systems supporting ˹(?i)˼, certainly in Perl, PHP, java.util.regex, Ruby (note: Ruby has a bug: ˹(?i)˼ sometimes fails on lowercase alternatives, but works when the alternatives are uppercase), and the .NET languages. Python and Tcl do not support ˹(?-i)˼, so the example does not work there.
In many implementations, except Python, an ˹(?i)˼ within parentheses applies only until the closing parenthesis. ˹(?i)˼ is therefore automatically turned off there, so the ˹(?-i)˼ can simply be omitted: ˹<B>(?:(?-i)sehr)<B>˼.
Switching modes within a regex is not limited to the /i modifier. Many systems support all modes in the following table. Some offer many additional modifiers, particularly PHP (see Pattern modifiers) and Tcl (see Tcl’s documentation).
Table: Mode modifiers.
Letter | Mode |
|---|---|
| Mode Case-insensitive |
| Mode Free-spacing |
| Mode Dot matches all |
| Mode Enhanced line anchors (multiline mode) |
Scoped mode modifiers: (?Modifikator:...), e.g. (?i:...)
The preceding section’s example using sehr can be simplified further if the system supports scoped mode changes. With ˹(?i:...)˼, the /i modifier applies only to the parenthesized subexpression. Thus, ˹<B>(?:(?i)sehr)<B>˼ becomes ˹<B>(?i:sehr)<B>˼.
Where supported, this form generally accepts all the letters in the table above. Tcl and Python support only the ˹(?i)˼ form, not the scoped form with ˹(?i:...)˼.
Comments: (?#...) and #...
Some dialects allow comments using ˹(?#...)˼. In practice, this is rarely used compared with free-spacing mode. Such comments are valuable in languages like VB.NET whose string literals have very limited functionality (see Searching and replacing in VB.NET, Regex options).
Literal text: \Q...\E
Within the sequence \Q...\E, all regex metacharacters become literal except \E itself. If there is no \E, this applies until the regex ends. This easily prevents ordinary text from being interpreted as a regex, especially when inserting a variable’s value into a regular expression.
For example, a web search form provides the user’s search term in $query, and we search for it with m/$query/i. Written this way, a search term such as ›C:\WINDOWS\‹ causes a runtime error because the trailing backslash makes an invalid regex.
This can be avoided with m/\Q$query\E/i, which transforms the inserted value ›C:\WINDOWS\‹ into ˹C\:\\WINDOWS\\˼, so the search finds ›C:\WINDOWS\‹, as the user probably intended.
This notation is less important with procedural or object-oriented systems (see Procedural and object-oriented approaches), since their regular expressions are ordinary strings. Before matching, a dedicated function can easily escape these strings for the regex engine. In VB, for example, use the Regex.Escape method (see Regex-Escape(String)); in PHP, use the preg_quote function (see preg_quote); in Java, use the quote method (see Additional Pattern methods).
As far as I know, the construct ˹\Q...\E˼ is fully supported only by the regex engines of java.util.regex and PCRE, and therefore PHP. Why not Perl, since the example used Perl? In Perl, \Q...\E is allowed only in regex literals , regular expressions written directly in program code, not in the contents of interpolated variables. For more, see Perl (see Properties of regex literals).
The implementation of ˹\Q...\E˼ in java.util.regex before Java 1.6.0 is faulty within character classes and should not be used.
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