Section 3.5. Common Metacharacters and Features

3.5. Common Metacharacters and Features

The remainder of this chapterthe next 30 pages or sooffers an overview of common regex metacharacters and concepts, as outlined on the next page. Not every issue is discussed, and no one tool includes everything presented here.

In one respect, this section is just a summary of much of what you've seen in the first two chapters, but in light of the wider, more complex world presented at the beginning of this chapter. During your first pass through, a light glance should allow you to continue on to the next chapters. You can come back here to pick up details as you need them.

Some tools add a lot of new and rich functionality and some gratuitously change common notations to suit their whim or special needs. Although I'll sometimes comment about specific utilities, I won't address too many tool-specific concerns here. Rather, in this section I'll just try to cover some common metacharacters and their uses, and some concerns to be aware of. I encourage you to follow along with the manual of your favorite utility.

Character Representations

˜115 Character Shorthands: \n, \t, \a, \b, \e, \f, \r, \v , ...

˜116 Octal Escapes : \ num

˜117 Hex/Unicode Escapes: \x num , \x { num } , \u num , \U num , ...

˜117 Control Characters : \c char

Character Classes and Class-Like Constructs

˜118 Normal classes: [a-z] and [^a-z]

˜119 Almost any character: dot

˜120 Exactly one byte: \C

˜120 Unicode Combining Character Sequence: \X

˜120 Class shorthands: \w, \d, \s, \W, \D, \S

˜121 Unicode properties, blocks, and categories: \p { Prop }, \P { Prop }

˜125 Class set operations: [[a-z] && [^aeiou]]

˜127 POSIX bracket -expression "character class": [ [:alpha:] ]

˜128 POSIX bracket-expression "collating sequences": [ [.span-ll.] ]

˜128 POSIX bracket-expression "character equivalents": [ [=n=] ]

˜128 Emacs syntax classes

Anchors and Other "Zero-Width Assertions"

˜129 Start of line/string: ^, \A

˜129 End of line/string: $, \Z, \z

˜130 Start of match (or end of previous match): \G

˜133 Word boundaries: \b, \B, \<, \> , ...

˜133 Lookahead ( ?= ‹), ( ?! ‹); Lookbehind, ( ?<= ‹), ( ?<! ‹)

Comments and Mode Modifiers

˜135 Mode modifier: (? modifier ) , such as (?i) or (?-i)

˜135 Mode-modified span: (? modifier :‹ ) , such as (?i :‹ )

˜136 Comments: (?# ) and #

˜136 Literal-text span: \Q \E

Grouping, Capturing, Conditionals, and Control

˜Capturing/grouping parentheses: (‹ ) , \1 , \2 , ...

˜137 Grouping-only parentheses: (?: )

˜138 Named capture: (? < Name >‹ )

˜139 Atomic grouping: (? >‹ )

˜139 Alternation :

˜140 Conditional: (? if then else )

˜141 Greedy quantifiers: *, +, ?, { num,num }

˜141 Lazy quantifiers: *?, +?, ??, { num,num }?

˜142 Possessive quantifiers: *+, ++, ?+, { num,num }+

3.5.1. Character Representations

This group of metacharacters provides visually pleasing ways to match specific characters that are otherwise difficult to represent. Character shorthands

Many utilities provide metacharacters to represent certain control characters that are sometimes machine-dependent , and which would otherwise be difficult to input or to visualize:


Alert (e.g., to sound the bell when "printed") Usually maps to the ASCII <BEL> character, 007 octal.


Backspace Usually maps to the ASCII <BS> character, 010 octal. (With many flavors, \b is a shorthand only within a character class, a word-boundary metacharacter outside ˜133.)


Escape character Usually maps to the ASCII <ESC> character, 033 octal.


Form feed Usually maps to the ASCII <FF> character, 014 octal.


Newline On most platforms (including Unix and DOS/Windows), usually maps to the ASCII <LF> character, 012 octal. On MacOS systems, usually maps to the ASCII <CR> character, 015 octal. With Java or any .NET language, always the ASCII <LF> character regardless of platform.


Carriage return Usually maps to the ASCII <CR> character. On MacOS systems, usually maps to the ASCII <LF> character. With Java or any .NET language, always the ASCII <CR> character regardless of platform.


Normal horizontal tab Maps to the ASCII <HT> character, 011 octal.


Vertical tab Usually maps to the ASCII <VT> character, 013 octal.

Table 3-6 lists a few common tools and some of the character shorthands they provide. As discussed earlier, some languages also provide many of the same shorthandsfor the string literals they support. Be sure to review that section (˜101) for some of the associated pitfalls. These are machine dependent?

As noted in the list, \n and \r are operating-system dependent in many tools, [ ] so, its best to choose carefully when you use them. When you need, for example, "a newline" for whatever system your script will happen to run on, use \n . When you need a character with a specific value, such as when writing code for a defined protocol like HTTP, use \012 or whatever the standard calls for. (\012 is an octal escape for the ASCII linefeed character.) If you wish to match DOS line ending characters, use \015\012 . To match either DOS or Unix line-ending characters, use \015?\012 . (These actually match the line-ending characters to match at the start or end of a line, use a line anchor ˜129).

[ ] If the tool itself is written in C or C++, and converts its regex backslash escapes into C backslash escapes, the resulting value is dependent upon the compiler. In practice, compilers for any particular platform are standardized around newline support, so its safe to view these as operating-system dependent. Furthermore, only \n and \r tend to vary across operating systems (and less so as time goes on), so the others can be considered standard across all systems.

Table 3-6. A Few Utilities and Some of the Shorthand Metacharacters They Provide Octal escape \num

Implementations supporting octal (base 8) escapes generally allow two- and three digit octal escapes to be used to indicate a byte or character with a particular value. For example, \015\012 matches an ASCII CR/LF sequence. Octal escapes can be convenient for inserting hard-to-type characters into an expression. In Perl, for instance, you can use \e for the ASCII escape character, but you cant in awk.

Since awk does support octal escapes, you can use the ASCII code for the escape character directly: \033 .

Table 3-7 on the next page shows the octal escapes some tools support.

Some implementations, as a special case, allow \0 to match a NUL byte. Some allow all one-digit octal escapes, but usually don't if backreferences such as \1 are supported. When theres a conflict, backreferences generally take precedence over octal escapes. Some allow four-digit octal escapes, usually to support a requirement that any octal escape begin with a zero (such as with java.util.regex ).

You might wonder what happens with out-of-range values like \565 (8-bit octal values range from \000 until only \377 ). It seems that half the implementations leave it as a larger-than-byte value (which may match a Unicode character if Unicode is supported), while the other half strip it to a byte. In general, it's best tolimit octal escapes to \377 and below. Hex and Unicode escapes: \xnum, \x{num}, \unum, \Unum, ...

Similar to octal escapes, many utilities allow a hexadecimal (base 16) value to be entered using \x , \u , or sometimes \U . If allowed with \x , for example, \x0D\x0A matches the CR/LF sequence. Table 3-7 shows the hex escapes that some tools support.

Besides the question of which escape is used, you must also know how many digits they recognize, and if braces may be (or must be) used around the digits. These are also indicated in Table 3-7. Control characters: \cchar

Many flavors offer the \c char sequence to match control characters with encoding values less than 32 (some allow a wider range). For example, \cH matches a Control-H, which represents a backspace in ASCII, while \cJ matches an ASCII linefeed (which is often also matched by \n , but sometimes by \r , depending on the platform ˜115).

Details aren't uniform among systems that offer this construct. You'll always be safe using uppercase English letters as in the examples. With most implementations, you can use lowercase letters as well, but Sun's Java regex package, for example, does not support them. And what exactly happens with non-alphabetics is very flavor-dependent, so I recommend using only uppercase letters with \c .

Related Note: GNU Emacs supports this functionality, but with the rather ungainly metasequence ?\^ char (e.g., ?\^H to match an ASCII backspace).

Table 3-7. A Few Utilities and the Octal and Hex Regex Escapes Their Regexes Support




Hex escapes


\0, \07,\377



\0, \77, \777



\0, \77, \777

\xF; \xFF; \x{‹}


\07, \077, \0377

\xFF; \uFFFF

GNU awk


\7, \77,\377


GNU sed



GNU Emacs




\0, \77,\377

\xFF, \uFFFF

PHP (preg suite)

\0, \77, \377

\xF, \xFF, \x{‹}





GNU egrep



GNU grep





\7, \77,\377

\xF, \xFF


\7, \77,\377

\xF, \xFF

\0 \0 matches a NUL byte, but other one-digit octal escapes are not supported

\7, \77 one- and two- digit octal escapes are supported

\07 two-digit octal escapes are supported if leading digit is a zero

\077 tHRee-digit octal escapes are supported if leading digit is a zero

\377 three-digit octal escapes are supported, until \377

\0377 four-digit octal escapes are supported, until \0377

\777 tHRee-digit octal escapes are supported, until \777

\x ‹ \x allows any number of digits

\x {‹} \x {‹} allows any number of digits

\xF, \xFF one- and two- digit hex escape is allowed with \x

\uFFFF four-digit hex escape allowed with \u

\UFFFF four-digit hex escape allowed with \U

\UFFFFFFFF eight-digit hex escape allowed with \U (See page 91 for version information.)

3.5.2. Character Classes and Class-Like Constructs

Modern flavors provide a number of ways to specify a set of characters allowed at a particular point in the regex, but the simple character class is ubiquitous. Normal classes: [a-z] and [^a-z]

The basic concept of a character class has already been well covered, but let me emphasize again that the metacharacter rules change depending on whether you're in a character class or not. For example, * is never a metacharacter within a class, while - usually is. Some metasequences , such as \b , sometimes have a different meaning within a class than outside of one (˜116).

With most systems, the order that characters are listed in a class makes no difference, and using ranges instead of listing characters is irrelevant to the execution speed (e.g., [0-9] should be no different from [9081726354] ). However, some implementations don't completely optimize classes (Sun's Java regex package comes to mind), so it's usually best to use ranges, which tend to be faster, wherever possible.

A character class is always a positive assertion . In other words, it must always match a character to be successful. A negated class must still match a character, but one not listed. It might be convenient to consider a negated character class to be a "class to match characters not listed." (Be sure to see the warning about dot and negated character classes, in the next section.) It used to be true that something like [^LMNOP] was the same as [\x00-KQ-\xFF] . In strictly eight-bit systems, it still is, but in a system such as Unicode where character ordinals go beyond 255 (\xFF) , a negated class like [^LMNOP] suddenly includes all the tens of thousands of characters in the encodingall except L, M, N, O , and P .

Be sure to understand the underlying character set when using ranges. For example, [a-Z] is likely an error, and in any case certainly isnt "alphabetics." One specification for alphabetics is [a-zA-Z] , at least for the ASCII encoding. (See \p{L} in "Unicode properties ˜121.) Of course, when dealing with binary data, ranges like ' \x80-\xFF ' within a class make perfect sense. Almost any character: dot

In some tools, dot is a shorthand for a character class that can match any character, while in most others, it is a shorthand to match any character except a newline . It's a subtle difference that is important when working with tools that allow target text to contain multiple logical lines (or to span logical lines, such as in a text editor). Concerns about dot include:

  • In some Unicode-enabled systems, such as Sun's Java regex package, dot normally does not match a Unicode line terminator (˜109).

  • A match mode (˜111) can change the meaning of what dot matches.

  • The POSIX standard dictates that dot not match a NUL (a character with the value zero), although all the major scripting languages allow NULLs in their text (and dot matches them). Dot versus a negated character class

When working with tools that allow multiline text to be searched, take care to note that dot usually does not match a newline, while a negated class like [^"] usually does. This could yield surprises when changing from something such as ".*" to "[^"]*" . The matching qualities of dot can often be changed by a match modesee "Dot-matches-all match mode on page 111. Exactly one byte

Perl and PCRE (and hence PHP) support \C , which matches one byte , even if that byte is one of several that might encode a single character (on the other hand, everything else works on a per- character basis). This is dangerousits misuse can cause internal errors, so it shouldn't be used unless you really know what you're doing. I can't think of a good use for it, so I won't mention it further. Unicode combining character sequence: \X

Perl and PHP support \X as a shorthand for \P{M}\p{M}* , which is like an extended . (dot). It matches a base character (anything not \p{M} ), possibly followed by any number of combining characters (anything that is \p{M} ).

As discussed earlier (˜107), Unicode uses a system of base and combining characters which, in combination, create what look like single, accented characters like   ('a' U+0061 combined with the grave accent '`' U+0300 ). You can use more than one combining character if that's what you need to create the final result. For example, if for some reason you need ' ', that would be 'c' followed by a combining cedilla '' and a combining breve ' ' ( U+0063 followed by U+0327 and U+0306 ).

If you wanted to match either "francais" or "fran ais," it wouldn't be safe to just use fran.ais or fran[c ]ais , as those assume that the ' ' is rendered with the single Unicode code point U+00C7 , rather than 'c followed by the cedilla ( U+0063 followed by U+0327 ). You could perhaps use fran(c,?)ais if you needed to be very specific, but in this case, fran \X ais is a good substitute for fran.ais .

Besides the fact that \X matches trailing combining characters, there are two differences between it and dot. One is that \X always matches a newline and other Unicode line terminators (˜109), while dot is subject to dot-matches-all match-mode (˜111), and perhaps other match modes depending on the tool. Another difference is that a dot-matches-all dot is guaranteed to match all characters at all times, while \X doesnt match a leading combining character. Class shorthands: \w, \d, \s, \W, \D, \S

Support for the following shorthands is common:


Digit Generally the same as [0-9] or, in some Unicode-enabled tools, all Unicode digits.


Non-digit Generally the same as [^\d]


Part-of-word character Often the same as [a-zA-Z0-9_] . Some tools omit the underscore , while others include all alphanumerics in the current locale (˜87). If Unicode is supported, \w usually refers to all alphanumerics; notable exceptions include java.util.regex and PCRE (and by extension, PHP), whose \w are exactly [a-zA-Z0-9_] .


Non-word character Generally the same as [^\w] .


Whitespace character On ASCII-only systems, this is often the same as [ \f\n\r\t\v] . Unicode-enabled systems sometimes also include the Unicode "next line control character U+0085 , and sometimes the "white space" property \p{Z} (described in the next section).


Non-whitespace character Generally the same as [^\s] .

As described on page 87, a POSIX locale could influence the meaning of these shorthands (in particular, \w ). Unicode-enabled programs likely have \w match a much wider scope of characters, such as \p{L} (discussed in the next section) plus an underscore. Unicode properties, scripts, and blocks: \p{Prop}, \P{Prop}

On its surface, Unicode is a mapping (˜106), but the Unicode Standard offers much more. It also defines qualities about each character, such as "this character is a lowercase letter," "this character is meant to be written right-to-left ," "this character is a mark that's meant to be combined with another character," etc.

Regular-expression support for these qualities varies, but many Unicode-enabled programs support matching via at least some of them with \p{ quality } (matches characters that have the quality) and \P{ quality } (matches characters without it). A basic example is \p{L} , where ' L ' is the quality meaning "letter (as opposed to number, punctuation, accents, etc.). ' L ' is an example of a general property (also called a category). We'll soon see other "qualities" that can be tested by \p{‹} and \P{‹} , but the most commonly supported are the general properties.

The general properties are shown in Table 3-8. Each character (each code point actually, which includes those that have no characters defined) can be matched by just one general property. The general property names are one character (' L ' for Letter, ' S ' for symbol, etc.), but some systems support a more descriptive synonym (' Letter ', ' Symbol ', etc.) as well. Perl, for example, supports these.

With some systems, single-letter property names may be referenced without the curly braces (e.g., using \pL instead of \p{L} ). Some systems may require (or simply allow) ' In ' or ' Is ' to prefix the letter (e.g., \p{IsL} ). As we look at additional qualities, we'll see examples of where an Is/In prefix is required. [ ]

[ ] As well see (and is illustrated in the table on page 125), the whole Is/In prefix business is somewhat of a mess. Previous versions of Unicode recommend one thing, while early implementations often did another. During Perl 5.8's development, I worked with the development group to simplifythings for Perl. The rule in Perl now is simply "You don't need to use ' Is ' or ' In ' unless you specifically want a Unicode Block (˜124), in which case you must prepend ' In '."

As shown in Table 3-9, each one-letter general Unicode property can be further subdivided into a set of two-letter sub-properties, such as "letter" being divided into "lowercase letter," "uppercase letter," "titlecase letter," "modifier letter," "other letter." Each code point is assigned exactly one of the subdivided properties.

Table 3-8. Basic Unicode Properties


Synonym and description

\p {L}

\p {Letter} Things considered letters.

\p {M}

\p {Mark} Various characters that are not meant to appear by themselves , but with other base characters (accent marks, enclosing boxes, ...).

\p {Z}

\p {Separator} Characters that separate things, but have no visual representation (various kinds of spaces ...).

\p {S}

\p {Symbol} Various types of Dingbats and symbols.

\p {N}

\p {Number} Any kind of numeric character.

\p {P}

\p {Punctuation} Punctuation characters.

\p {C}

\p {Other} Catch-all for everything else (rarely used for normal characters).

Additionally, some implementations support a special composite sub-property, \p{L&} , which is a shorthand for all "cased" letters, and is the same as [\p{Lu}\p{Ll}\p{Lt}] .

Also shown in Table 3-9 are the full-length synonyms (e.g., " Lowercase_Letter " instead of " Ll ") supported by some implementations. The standard suggests that a variety of forms be accepted (such as ' LowercaseLetter ', ' LOWERCASE_LETTER ', ' Lowercase Letter ', ' lowercase-letter ', etc.), but I recommend, for consistency, always using the form shown in Table 3-9.

Scripts . Some systems have support for matching via a script (writing system) name with \p{‹} . For example, if supported, \p{Hebrew} matches characters that are specifically part of the Hebrew writing system. (A script does not include common characters that might be used by other writing systems as well, such as spaces and punctuation.)

Some scripts are language-based (such as Gujarati, Thai, Cherokee , ‹). Some span multiple languages (e.g., Latin, Cyrillic ), while some languages are composed of multiple scripts, such as Japanese, which uses characters from the Hiragana, Katakana, Han ("Chinese Characters"), and Latin scripts. See your system's documentation for the full list.

A script does not include all characters used by the particular writing system, but rather, all characters used only (or predominantly) by that writing system. Common characters such as spacing and punctuation are not included within any script, but rather are included as part of the catch-all pseudo-script IsCommon , matched by \p{IsCommon} . A second pseudo-script, Inherited , is composed of certain combining characters that inherit the script from the base character that they follow.

Table 3-9. Basic Unicode Sub-Properties


Synonym and description

\p {Ll}

\p {Lowercase_Letter} Lowercase letters.

\p {Lu}

\p {Uppercase_Letter} Uppercase letters.


\p {Titlecase_Letter} Letters that appear at the start of a word (e.g., the character D is the title case of the lowercase d and of the uppercase D ).

\p {L&}

A composite shorthand matching all \p {Ll} , \p {Lu} , and \p {Lt} characters.

\p {Lm}

\p {Modifier_Letter} A small set of letter-like special-use characters.

\p {Lo}

\p {Other_Letter} Letters that have no case, and aren't modifiers, including letters from Hebrew, Arabic, Bengali, Tibetan, Japanese, ...

\p {Mn}

\p {Non_Spacing_Mark} "Characters" that modify other characters, such as accents, umlauts, certain "vowel signs," and tone marks.

\p {Mc}

\p {Spacing_Combining_Mark} Modification characters that take up space of their own (mostly "vowel signs" in languages that have them, including Bengali, Gujarati, Tamil, Telugu, Kannada, Malayalam, Sinhala, Myanmar, and Khmer).

\p {Me}

\p {Enclosing_Mark} A small set of marks that can enclose other characters, such as circles, squares, diamonds, and "keycaps."

\p {Zs}

\p {Space_Separator} Various kinds of spacing characters, such as a normal space, non-break space, and various spaces of specific widths.

\p {Zl}

\p {Line_Separator} The LINE SEPARATOR character ( U+2028 ).

\p {Zp}

\p {Paragraph_Separator} The PARAGRAPH SEPARATOR character ( U+2029 ).

\p {Sm}

\p {Math_Symbol} +, ·, a fraction slash, , ...

\p {Sc}

\p {Currency_Symbol} $, , , ‚, ...

\p {Sk}

\p {Modifier_Symbol} Mostly versions of the combining characters, but as full-fledged characters in their own right.

\p {So}

\p {Other_Symbol} Various Dingbats, box-drawing symbols, Braille patterns, non-letter Chinese characters, ...

\p {Nd}

\p {Decimal_Digit_Number} Zero through nine, in various scripts (not including Chinese, Japanese, and Korean).

\p {Nl}

\p {Letter_Number} Mostly Roman numerals.

\p {No}

\p {Other_Number} Numbers as superscripts or symbols; characters representing numbers that aren't digits (Chinese, Japanese, and Korean not included).

\p {Pd}

\p {Dash_Punctuation} Hyphens and dashes of all sorts.

\p {Ps}

\p {Open_Punctuation} Characters like (, ½, and , ...

\p {Pe}

\p {Close_Punctuation} Characters like), ‹, ‹, ...

\p {Pi}

\p {Initial_Punctuation} Characters like , ', <, ...

\p {Pf}

\p {Final_Punctuation} Characters like , ', >, ...

\p {Pc}

\p {Connector_Punctuation} A few punctuation characters with special linguistic meaning, such as an underscore.

\p {Po}

\p {Other_Punctuation} Catch-all for other punctuation: !, &, ., :, :, ...

\p {Cc}

\p {Control} The ASCII and Latin-1 control characters ( TAB, LF, CR , ...)

\p {Cf}

\p {Format} Non-visible characters intended to indicate some basic formatting( zero width joiner, activate Arabic form shaping , ...)

\p {Co}

\p {Private_Use} Code points allocated for private use (company logos, etc.).

\p {Cn}

\p { Unassigned } Code points that have no characters assigned.

Blocks . Similar (but inferior) to scripts, blocks refer to ranges of code points on the Unicode character map. For example, the Tibetan block refers to the 256 code points from U+0F00 through U+0FFF . Characters in this block are matched with \p{InTibetan} in Perl and java.util.regex , and with \p{IsTibetan} in .NET. (More on this in a bit.)

There are many blocks, including blocks for most systems of writing ( Hebrew, Tamil, Basic_Latin, Hangul_Jamo, Cyrillic, Katakana , ...), and for special character types ( Currency, Arrows, Box_Drawing, Dingbats , ...).

Tibetan is one of the better examples of a block, since all characters in the block that are defined relate to the Tibetan language, and there are no Tibetan-specific characters outside the block. Block qualities, however, are inferior to script qualities for a number of reasons:

  • Blocks can contain unassigned code points. For example, about 25 percent of the code points in the Tibetan block have no characters assigned to them.

  • Not all characters that would seem related to a block are actually part of that block. For example, the Currency block does not contain the universal currency symbol '', nor such notable currency symbols as $, , & pound ;, ‚, and . (Luckily, in this case, you can use the currency property, \p{Sc} , in its place.)

  • Blocks often have unrelated characters in them. For example, (Yen symbol) is found in the Latin_1_Supplement block.

  • What might be considered one script may be included within multiple blocks . For example, characters used in Greek can be found in both the Greek and Greek_Extended blocks .

Support for block qualities is more common than for script qualities. There is ample room for getting the two confused because there is a lot of overlap in the naming (for example, Unicode provides for both a Tibetan script and a Tibetan block).

Furthermore, as Table 3-10 on the facing page shows, the nomenclature has not yet been standardized. With Perl and java.util.regex , the Tibetan block is \p{ In Tibetan} , but in the .NET Framework, its \p{ Is Tibetan} (which, to add to the confusion, Perl allows as an alternate representation for the Tibetan script ).

Other properties/qualities . Not everything talked about so far is universally supported. Table 3-10 gives a few details about what's been covered so far.

Additionally, Unicode defines many other qualities that might be accessible via the \p{‹} construct, including ones related to how a character is written ( left-to-right , right-to-left, etc.), vowel sounds associated with characters, and more. Some implementations even allow you to create your own properties on the fly. See your programs documentation for details on what's supported.

Table 3-10. Property/Script/Block Features






Basic Properties like \p{L}

Basic Properties shorthand like \pL


Basic Properties longhand like \p{IsL}



Basic Properties full like \p{Letter}




Composite \p{L&}



Script like \p{Greek}



Script longhand like \p{ Is Greek}




Block like \p{Cyrillic}

if no script



Block longhand like \p{ In Cyrillic}



Block longhand like \p{ Is Cyrillic}




Negated \P{‹}

Negated \p{^‹}




as \p{all}



as \P{Cn}

as \P{Cn}

as \P{Cn}


as \p{Cn}

as \p{Cn}

as \p{Cn}

Lines checkmarked at left are recommended for new implementations. (See page 91 for version information) Simple class subtraction: [[a-z]-[aeiou]]

.NET offers a simple class "subtraction" nomenclature, which allows you to remove from what a class can match those characters matchable by another class. For example, the characters matched by [[a-z]-[aeiou]] are those matched by [a-z] minus those matched by [aeiou] , i.e. that are non-vowel lower-case ASCII.

As another example, [\p{P}-[\p{Ps}\p{Pe}]] is a class that matches characters in \p{P} except those matchable by [\p{Ps}\p{Pe}] , which is to say that it matches all punctuation except opening and closing punctuation such as ‹and (. Full class set operations: [[a-z] && [^aeiou]]

Sun's Java regex package supports a full range of set operations (union, subtraction, intersection) within character classes. The syntax is different from the simple class subtraction mentioned in the previous section (and, in particular, Java's set subtraction looks particularly oddthe non-vowel example shown in the previous section would be rendered in Java as [ [a-z] && [^aeiou] ] ). Before looking at subtraction in detail, let's look at the two basic class set operations, OR and AND.

OR allows you to add characters to the class by including what looks like an embedded class within the class: [abcxyz] can also be written as [ [abc][xyz] ] , [ abc[xyz] ] , or [ [abc]xyz ] , among others. OR combines sets, creating a new set that is the sum of the argument sets. Conceptually, it's similar to the "bitwise or" operator that many languages have via a ' ' or ' or ' operator. In character classes, OR is mostly a notational convenience, although the ability to include negated classes can be useful in some situations.

AND does a conceptual "bitwise AND" of two sets, keeping only those characters found in both sets. It is achieved by inserting the special class metasequence && between two sets of characters. For example, [\p{InThai}&&\P{Cn}] matches all assigned code points in the Thai block. It does this by taking the intersection between (i.e., keeping only characters in both) \p{InThai} and \P{Cn} . Remember, \P{ } with a capital 'P', matches everything not part of the quality, so \P{Cn} matches everything not unassigned , which in other words, means is assigned . (Had Sun supported the Assigned quality, I could have used \p{Assigned} instead of \P{Cn} in this example.)

Be careful not to confuse OR and AND. How intuitive these names feel depends on your point of view. For example, [ [this][that] ] in normally read "accept characters that match [this] or [that] ," yet it is equally true if read "the list of characters to allow is [this] and [that] ." Two points of view for the same thing.

AND is less confusing in that [\p{InThai}&&\P{Cn}] is normally read as "match only characters matchable by \p{InThai} and \P{Cn} ," although it is sometimes read as "the list of allowed characters is the intersection of \p{InThai} and \P{Cn} ."

These differing points of view can make talking about this confusing: what I call OR and AND, some might choose to call AND and INTERSECTION.

Class subtraction with set operators . It's useful to realize that \P{Cn} is the same as [^\p{Cn}] , which allows the "assigned characters in the Thai block" example, [\p{InThai}&&\P{Cn}] , to be rewritten as [\p{InThai}&&[^\p{Cn}]] . Such a change is not particularly helpful except that it helps to illustrate a general pattern: realizing that "assigned characters in the Thai block" can be rephrased as the somewhat unruly "characters in the Thai block, minus un assigned characters," we then see that [ &&[ ^ ]] means " minus ."

This brings us back to the [ [a-z] && [^aeiou] ] example from the start of the section, and shows how to do class subtraction . The pattern is that [this &&[^that]] means " [ this ] minus [ that ] ." I find that the double negatives of && and [^‹] tend to make my head swim, so I just remember the [‹ && [ ^‹ ] ] pattern.

Mimicking class set operations with lookaround . If your program doesn't support class set operations, but does support lookaround (˜133), you can mimic the set operations. With lookahead, [ &&[^ ] ] can be rewritten as (? ! ) . [ ] Although not as efficient as well-implemented class set operations, using lookaround can be quite flexible. This example can be written four different ways (substituting IsThai for InThai in .NET ˜125):

[ ] Actually, in Perl, this particular example could probably be written simply as \p{Thai} , since in Perl \p{Thai} is a script , which never contains unassigned code points. Other differences between the Thai script and block are subtle. It's beneficial to have the documentation as to what is actually covered by any particular script or block. In this case, the script is actually missing a few special characters that are in the block. See the data files at for all the details.

 (?!\p{Cn})\p{InThai}         (?=\P{Cn})\p{InThai}         \p{InThai}(?<!\p{Cn})         \p{InThai}(?<=\P{Cn}) POSIX bracket-expression "character class" : [[:alpha:]]

What we normally call a character class , the POSIX standard calls a bracket expression . POSIX uses the term "character class" for a special feature used within a bracket expression [ ] that we might consider to be the precursor to Unicodes character properties.

[ ] In general, this book uses "character class and "POSIX bracket expression" as synonyms to refer to the entire construct, while "POSIX character class" refers to the special range-like class feature described here.

A POSIX character class is one of several special metasequences for use within a POSIX bracket expression. An example is [:lower:] , which represents any lowercase letter within the current locale (˜87). For English text, [:lower:] is comparable to a-z . Since this entire sequence is valid only within a bracket expression, the full class comparable to [ a-z ] is [ [:lower:] ] . Yes, its that ugly. But, it has the advantage over [a-z] of including other characters, such as , ±, and the like if the locale actually indicates that they are "lowercase letters."

The exact list of POSIX character classes is locale dependent, but the following are usually supported:


alphabetic characters and numeric character


alphabetic characters


space and tab


control characters




non-blank characters (not spaces, control characters, or the like)


lowercase alphabetics


like [:graph:] , but includes the space character


punctuation characters


all whitespace characters ( [:blank:] , newline, carriage return, and the like)


uppercase alphabetics


digits allowed in a hexadecimal number (i.e., 0-9a-fA-F ).

Systems that support Unicode properties (˜121) may or may not extend that Unicode support to these POSIX constructs. The Unicode property constructs are more powerful, so those should generally be used if available. POSIX bracket-expression "collating sequences" : [[.span-ll.]]

A locale can have collating sequences to describe how certain characters or sets of characters should be ordered. For example, in Spanish, the two characters ll (as in tortilla ) traditionally sort as if they were one logical character between l and m , and the German is a character that falls between s and t , but sorts as if it were the two characters ss . These rules might be manifested in collating sequences named, for example, span-ll and eszet .

A collating sequence that maps multiple physical characters to a single logical character, such as the span-ll example, is considered "one character" to a fully compliant POSIX regex engine. This means that [^abc] matches a ' ll ' sequence.

A collating sequence element is included within a bracket expression using a[.‹.] notation: torti [ [.span-ll.] ] a matches tortilla . A collating sequence allows you to match against those characters that are made up of combinations of other characters. It also creates a situation where a bracket expression can match more than one physical character. POSIX bracket-expression "character equivalents" : [[=n=]]

Some locales define character equivalents to indicate that certain characters should be considered identical for sorting and such. For example, a locale might define an equivalence class ' n ' as containing n and ± , or perhaps one named ' a ' as containing a ,   , and . Using a notation similar to [: :] , but with '=' instead of a colon , you can reference these equivalence classes within a bracket expression: [ [=n=][=a=] ] matches any of the characters just mentioned.

If a character equivalence with a single-letter name is used but not defined in the locale, it defaults to the collating sequence of the same name. Locales normally include normal characters as collating sequences [.a.], [.b.], [.c.] , and so onso in the absence of special equivalents, [ [=n=][=a=] ] defaults to [na] . Emacs syntax classes

GNU Emacs doesn't support the traditional \w , \s , etc.; rather, it uses special sequences to reference "syntax classes :

\s char

matches characters in the Emacs syntax class as described by char

\S char

matches characters not in the Emacs syntax class

\sw matches a "word constituent character, and \s- matches a "whitespace character." These would be written as \w and \s in many other systems.

Emacs is special because the choice of which characters fall into these classes can be modified on the fly, so, for example, the concept of which characters are word constituents can be changed depending upon the kind of text being edited.

3.5.3. Anchors and Other "Zero-Width Assertions"

Anchors and other "zero-width assertions" don't match actual text, but rather positions in the text. Start of line/string: ^, \A

Caret ^ matches at the beginning of the text being searched, and, if in an enhanced line-anchor match mode (˜112), after any newline. In some systems, an enhanced-mode ^ can match after Unicode line terminators, as well (˜109).

When supported, \A always matches only at the start of the text being searched, regardless of any match mode. End of line/string: $, \Z, \z

As Table 3-11 on the next page shows, the concept of "end of line" can be a bit more complex than its start-of-line counterpart . $ has a variety of meanings among different tools, but the most common meaning is that it matches at the end of the target string, and before a string-ending newline, as well. The latter is common, to allow an expression like s$ (ostensibly, to match "a line ending with s) to match '... s ', a line ending with s that's capped with an ending newline.

Two other common meanings for $ are to match only at the end of the target text, and to match before any newline. In some Unicode systems, the special meaning of newline in these rules are replaced by Unicode line terminators (˜109). (Java, for example, offers particularly complex semantics for $ with respect to Unicode line terminators ˜370.)

A match mode (˜112) can change the meaning of $ to match before any embedded newline (or Unicode line terminator as well).

When supported, \Z usually matches what the "unmoded $ matches, which often means to match at the end of the string, or before a string-ending newline. To complement these, \Z matches only at the end of the string, period, without regard to any newline. See Table 3-11 for a few exceptions.

Table 3-11. Line Anchors for Some Scripting Languages

















^ matches at start of string

^matches after any newline








$ matches at end of string

$ matches before string-ending newline



$ matches before any newline








Has enhanced line-anchor mode (˜112)


In enhanced line-anchor mode ...








^ matches at start of string


^ matches after any newline



$ matches at end of string


$ matches before any newline



\A always matches like normal ^


\Z always matches like normal $




\z always matches only at end of string




(See page 91 for version information.)

[] Notes: 1. Sun's Java regex package supports Unicode's line terminator (˜109) in these cases.

[] 2. Ruby's $ and ^ match at embedded newlines, but its \A and \Z do not.

[] 3. Python's \Z matches only at the end of the string.

[] 4. Ruby's \A, unlike its ^, matches only at the start of the string.

[] 5. Ruby's \Z, unlike its $, matches at the end of the string, or before a string-ending newline. Start of match (or end of previous match): \G

\G was first introduced by Perl to be useful when doing iterative matching with /g (˜51), and ostensibly matches the location where the previous match left off. On the first iteration, \G matches only at the beginning of the string, just like \A .

If a match is not successful, the location at which \G matches is reset back to the beginning of the string. Thus, when a regex is applied repeatedly, as with Perls s/‹/‹ /g or other languages "match all" function, the failure that causes the "match all" to fail also resets the location for \G for the next time a match of some sort is applied.

Perl's \G has three unique aspects that I find quite interesting and useful:

  • The location associated with \G is an attribute of each target string , not of the regexes that are setting that location. This means that multiple regexes can match against a string, in turn , each using \G to ensure that they pick up exactly where one of the others left off.

  • Perl's regex operators have an option (Perl's /c modifier ˜315) that indicates a failing match should not reset the \G location, but rather to leave it where it was. This works well with the first point to allow tests with a variety of expressions to be performed at one point in the target string, advancing only when theres a match.

  • That location associated with \G can be inspected and modified by non-regex constructs (Perls pos function ˜313). One might want to explicitly set the location to "prime" a match to start at a particular location, and match only at that location. Also, if the language supports this point, the functionality of the previous point can be mimicked with this feature, if it's not already supported directly.

See the sidebar on the next page for an example of these features in action. Despite these convenient features, Perl's \G does have a problem in that it works reliably only when its the first thing in the regex. Luckily, that's where it's most-naturally used. End of previous match, or start of the current match?

One detail that differs among implementations is whether \G actually matches the "start of the current match or "end of the previous match." In the vast majority of cases, the two meanings are the same, so it's a non-issue most of the time. Uncommonly, they can differ . There is a realistic example of how this might arise on page 215, but the issue is easiest to understand with a contrived example: consider applying x? to ' abcde '. The regex can match successfully at ' abcde ', but doesnt actually match any text. In a global search-and-replace situation, where the regex is applied repeatedly, picking up each time from where it left off, unless the transmission does something special, the "where it left off" will always be the same as where it started. To avoid an infinite loop, the transmission forcefully bumps along to the next character (˜148) when it recognizes this situation. You can see this by applying s/x?/!/g to ' abcde ', yielding ' !a!b!c!d!e! '.

One side effect of the transmission having to step in this way is that the "end of the previous match" then differs from "the start of the current match." When this happens, the question becomes: which of the two locations does \G match? In Perl, actually applying s/\Gx?/!/g to ' abcde ' yields just ' !abcde ', so in Perl, \G really does match only the end of the previous match. If the transmission does the artificial bump-along, Perls \G is guaranteed to fail.

On the other hand, applying the same search and replace with some other tools yields the original ' !a!b!c!d!e! ', showing that their \G matches successfully at the start of each current match, as decided after the artificial bump-along.

You can't always rely on the documentation that comes with a tool to tell you which is which, as both Microsoft's .NET and Sun's Java documentation were incorrect until I contacted them about it (they've since been fixed). The status now is that PHP and Ruby have \G match at the start of the current match, while Perl, java.util.regex , and the .NET languages have it match at the end of the previous match.

Advanced Use of \G with Perl

Here's the outline of a snippet that performs simple validation on the HTML in the variable $html , ensuring that it contains constructs from among only a very limited subset of HTML (simple <IMG> and <A> tags are allowed, as well as simple entities like &gt; ). I used this method at Yahoo!, for example, to validate that a user 's HTML submission met certain guidelines.

This code relies heavily on the behavior of Perl's m/‹/gc match operator, which applies the regular expression to the target string once, picking up from where the last successful match left off, but not resetting that position if it fails (˜315).

Using this feature, the various expressions used below all "tag team" to work their way through the string. It's similar in theory to having one big alternation with all the expressions, but this approach allows program code to be executed with each match, and to include or exclude expressions on the fly.

 my $need_close_anchor = 0;  # True if we've seen <A>, but not its closing </A>  .    while (not $html =~ m/\G\z/gc) #  While we haven't worked our way to the end  ...    {      if ($html =~ m/  \G(\w+)  /gc) {        ...  have a word or number in  -- can now check for profanity, for example  ...      } elsif ($html =~ m/  \G[^<>&\w]  +/gc) {        #  Other non-HTML stuff -- simply allow it  .      } elsif ($html =~ m/  \G<img\s+([^>]+)>  /gci) {        ...  have an image tag -- can check that it's appropriate  ...  } elsif (not $need_close_anchor and $html =~ m/  \G<A\s+([^>]+)>  /gci) {        ...  have a link anchor  can validate it  ...  $need_close_anchor = 1; #  Note that we now need </A>  } elsif ($need_close_anchor and $html =~ m{  \G</A>  }gci){        $need_close_anchor = 0; #  Got what we needed; don't allow again  } elsif ($html =~ m/  \G&(#\d+<\w+);  /gc){        #  Allow entities like &gt; and &#123;  } else {#  Nothing matched at this point, so it must be an error. Note the location, and grab a dozen or so  #  characters from the HTML so that we can issue an informative error message  .        my $location = pos($html); #  Note where the unexpected HTML starts  .        my ($badstuff) = $html =~ m/  \G(.{1,12})  /s;        die "Unexpected HTML at position $location: $badstuff\n";      }    }    #  Make sure there's no dangling <A>  if ($need_close_anchor) {      die "Missing final </A>"   } Word boundaries: \b, \B, \<, \>, ...

Like line anchors, word-boundary anchors match a location in the string. There are two distinct approaches. One provides separate metasequences for start - and end of-word boundaries (often \< and \>), while the other provides ones catch-all word boundary metasequence (often \b ). Either generally provides a not-word boundary metasequence as well (often \B ). Table 3-12 shows a few examples. Tools that don't provide separate start- and end-of-word anchors, but do support lookaround, can mimic word-boundary anchors with the lookaround. In the table, I've filled in the otherwise empty spots that way, wherever practical.

A word boundary is generally defined as a location where there is a "word character" on one side, and not on the other. Each tool has its own idea of what constitutes a "word character," as far as word boundaries go. It would make sense if the word boundaries agree with \w , but that's not always the case. With PHP and java.util.regex , for example, \w applies only to ASCII characters and not the full breadth of Unicode, so in the table I've used lookaround with the Unicode letter property \pL (which is a shorthand for \p{L} ˜121).

Whatever the word boundaries consider to be "word characters," word boundary tests are always a simple test of adjoining characters. No regex engine actually does linguistic analysis to decide about words: all consider "NE14AD8" to be a word, but not "M.I.T." Lookahead (?=‹), (?!‹) ; Lookbehind, (?<=‹), (?<!‹)

Lookahead and lookbehind constructs (collectively, lookaround ) are discussed with an extended example in the previous chapter's "Adding Commas to a Number with Lookaround" (˜59). One important issue not discussed there relates to what kind of expression can appear within either of the lookbehind constructs. Most implementations have restrictions about the length of text matchable within lookbehind (but not within lookahead, which is unrestricted).

The most restrictive rule exists in Perl and Python, where the lookbehind can match only fixed-length strings. For example, (?<!; \w ) and (?<! thisthat ) are allowed, but (?<! books? ) and (?<^ \w+: ) are not, as they can match a variable amount of text. In some cases, such as with (?<! books? ) , you can accomplish the same thing by rewriting the expression, as with (?<!book)(?<!books) , although thats certainly not easy to read at first glance.

Table 3-12. A Few Utilities and Their Word Boundary Metacharacters


Start-of-word ... End-of-word

Word boundary

Not word-boundary

GNU awk

\< ...\>



GNU egrep

\< ...\>



GNU Emacs

\< ...\>




(?<!\pL)(?=\pL) ... (?<=\pL)(?!\pL)




[[:<:]] ... [[:>:]]




(?<!\w)(?=\w) ... (?<=\w)(?!\w)




(?<!\w)(?=\w) ... (?<=\w)(?!\w)




(?<!\pL)(?=\pL) ... (?<=\pL)(?!\pL)




(?<!\w)(?=\w) ... (?<=\w)(?!\w)







GNU sed

\< ...\>




\m ... \M



Items marked with work only with characters in ASCII (or the locale-based 8-bit encoding), even though the flavor otherwise supports Unicode.

(See page 91 for version infomation.)

The next level of support allows alternatives of different lengths within the look behind, so (?< !books? ) can be written as (?< !bookbooks ) . PCRE (and as such the preg suite in PHP) allows this.

The next level allows for regular expressions that match a variable amount of text, but only if it's of a finite length. This allows (?< !books? ) directly, but still disallows (?<!^ \w+: ) since the \w+ is open -ended. Sun's Java regex package supports this level.

When it comes down to it, these first three levels of support are really equivalent, since they can all be expressed , although perhaps somewhat clumsily, with the most restrictive fixed-length matching level of support. The intermediate levels are just "syntactic sugar" to allow you to express the same thing in a more pleasing way. The fourth level, however, allows the subexpression within lookbehind to match any amount of text, including the (?<!^ \w+: ) example. This level, supported by Microsoft's .NET languages, is truly superior to the others, but does carry a potentially huge efficiency penalty if used unwisely. (When faced with lookbehind that can match any amount of text, the engine is forced to check the lookbehind subexpression from the start of the string, which may mean a lot of wasted effort when requested from near the end of a long string.)

3.5.4. Comments and Mode Modifiers

With many flavors, the regex modes and match modes described earlier (˜110) can be modified within the regex (on the fly, so to speak) by the following constructs. Mode modifier: (?modifier) , such as (?i) or (?-i)

Many flavors now allow some of the regex and match modes (˜110) to be set within the regular expression itself. A common example is the special notation (?i) , which turns on case-insensitive matching, and (?-i) , which turns it off. For example, <B>(?i)very(?-i)</B> has the very part match with case insensitivity, while still keeping the tag names case-sensitive. This matches ' <B>VERY</B> ' and ' <B>Very</B> ', for example, but not ' <b>Very</b> '.

This example works with most systems that support (?i) , including Perl, PHP, java.util.regex , Ruby, [ ] and the .NET languages. It doesnt work with Python or Tcl, neither of which support (?-i) .

[ ] The example works with Ruby, but note that Rubys (?i) has a bug whereby it sometimes doesn't work with -separated alternatives that are lowercase (but does if theyre uppercase).

With most implementations except Python, the effects of (?i) within any type of parentheses are limited by the parentheses (that is, turn off at the closing parentheses). So, the (?-i) can be eliminated by wrapping the case-insensitive part in parentheses and putting (?i) as the first thing inside: <B> (?: (?i)very ) </B> .

The mode-modifier constructs support more than just ' i '. With most systems, you can use at least those shown in Table 3-13. Some systems have additional letters for additional functions. PHP, in particular, offers quite a few extra (˜446), as does Tcl (see its documentation).

Table 3-13. Common Mode Modifiers




case-insensitivity match mode (˜110)


free-spacing and comments regex mode (˜111)


dot-matches-all match mode (˜111)


enhanced line-anchor match mode (˜112) Mode-modified span: (?modifier :‹) , such as (?i:‹)

The example from the previous section can be made even simpler for systems that support a mode-modified span. Using a syntax like (?i:‹) , a mode-modified span turns on the mode only for whats matched within the parentheses. Using this, the <B> (?:(?i) very ) </B> example is simplified to <B> (?i: very ) </B> . When supported, this form generally works for all mode-modifier letters the system supports. Tcl and Python are two examples that support the (?i) form, but not the mode-modified span (?i:‹) form. Comments: (?#‹) and #‹

Some flavors support comments via (?#‹) . In practice, this is rarely used, in favor of the free-spacing and comments regex mode (˜111). However, this type of comment is particularly useful in languages for which its difficult to get a newline into a string literal, such as VB.NET (˜99, 420). Literal-text span: \Q‹\E

First introduced with Perl, the special sequence \Q‹\E turns off all regex metacharacters between them, except for \E itself. (If the \E is omitted, they are turned off until the end of the regex.) It allows what would otherwise be taken as normal metacharacters to be treated as literal text. This is especially useful when including the contents of a variable while building a regular expression.

For example, to respond to a web search, you might accept what the user types as $query , and search for it with m/$query/i . As it is, this would certainly have unexpected results if $query were to contain, say, ' C:\WINDOWS\ ', which results in a run-time error because the search term contains something that isn't a valid regular expression (the trailing lone backslash).

\Q‹\E avoids the problem. With the Perl code m/\Q$query\E/i , a $query of ' C:\WINDOWS\ ' becomes C\:\\WINDOWS\\ , resulting in a search that finds the original ' C:\WINDOWS\ ' as the user expects.

This feature is less useful in systems with procedural and object-oriented handling (˜95), as they accept normal strings. While building the string to be used as a regular expression, it's fairly easy to call a function to make the value from the variable "safe" for use in a regular expression. In VB, for example, one would use the Regex.Escape method (˜432); PHP has the preg_quote function (˜470); Java has a quote method (˜395).

The only regex engines that I know of that support \Q‹\E are java.util.regex and PCRE (and hence also PHPs preg suite). Considering that I just mentioned that this was introduced with Perl (and I gave an example in Perl), you might wonder why I don't include Perl in the list. Perl supports \Q‹\E within regex literals (regular expressions appearing directly in the program), but not within the contents of variables that might be interpolated into them. See Chapter 7 (˜290) for details.

The java.util.regex support for \Q‹\E within a character class, prior to Java 1.6.0, is buggy and shouldnt be relied upon.

3.5.5. Grouping, Capturing, Conditionals, and Control Capturing/Grouping Parentheses: (‹) and \1, \2, ...

Common, unadorned parentheses generally perform two functions, grouping and capturing. Common parentheses are almost always of the form (‹) , but a few flavors use \(‹\) . These include GNU Emacs, sed , vi , and grep .

Capturing parentheses are numbered by counting their opening parentheses from the left, as shown in figures on pages 41, 43, and 57. If backreferences are available, the text matched via an enclosed subexpression can itself be matched later in the same regular expression with \1 , \2 , etc.

One of the most common uses of parentheses is to pluck data from a string. The text matched by a parenthesized subexpression (also called "the text matched by the parentheses") is made available after the match in different ways by different programs, such as Perl's $1 , $2 , etc. (A common mistake is to try to use the \1 syntax outside the regular expression; something allowed only with sed and vi .) Table 3-14 on the next page shows how a number of programs make the captured text available after a match. It shows how to access the text matched by the whole expression, and the text matched within a set of capturing parentheses. Grouping-only parentheses: (?:‹)

Grouping-only parentheses (?: ) dont capture, but, as the name implies, group regex components for alternation and the application of quantifiers. They are not counted as part of $1 , $2 , etc. After a match of (1one) (?: andor)(2two ) , for example, $1 contains ' 1 ' or ' one ', while $2 contains ' 2 ' or ' two '. Grouping-only parentheses are also called non-capturing parentheses.

Non-capturing parentheses are useful for a number of reasons. They can help make the use of a complex regex clearer in that the reader doesn't need to wonder if what's matched by what they group is accessed elsewhere by $1 or the like. Also, they can be more efficient. If the regex engine doesn't need to keep track of the text matched for capturing purposes, it can work faster and use less memory. (Efficiency is covered in detail in Chapter 6.)

Non-capturing parentheses are useful when building up a regex from parts . Recall the example from page 76 in which the variable $HostnameRegex holds a regex to match a hostname. Imagine using that to pluck out the whitespace around a hostname, as in the Perl snippet m/ (\s*)$HostnameRegex(\s*) / . After this, you might expect $1 and $2 to hold the leading and trailing whitespace, but the trailing whitespace is actually in $4 because $HostnameRegex contains two sets of capturing parentheses:

 $HostnameRegex = qr/[-a-z0-9]+(\.[-a-z0-9]+)*\.(comeduinfo)/i; 

Table 3-14. A Few Utilities and Their Access to Captured Text


Entire match

First set of parentheses

GNU egrep



GNU Emacs

(match-string 0) (\& within replacement string)

(match-string 1) (\1 within replacement string)

GNU awk

substr($text, RSTART, RLENGTH) (\& within replacement string)

\1 (within gensub replacement)




Perl ˜41



PHP ˜450

$ matches [0]

$ matches [1]

Python ˜97

MatchObj .group(0)

MatchObj .group(1)




GNU sed

& ( in replacement string only )

\1 (in replacement and regex only)

Java ˜95

MatcherObj .group()


set to user-selected variables via regexp command

VB.NET ˜96

MatchObj .Groups(0)

MatchObj .Groups(1)


MatchObj .Groups[0]

MatchObj .Groups[1]




(See page 91 for version information.)

Were those sets of parentheses non-capturing instead, $HostnameRegex could be used without generating this surprise. Another way to avoid the surprise, although not available in Perl, is to use named capture, discussed next. Named capture: (?<Name>‹)

Python, PHP's preg engine, and .NET languages support captures to named locations. Python and PHP use the syntax (?P<name>‹) , while the .NET languages use (?<name>‹) , a syntax that I prefer. Heres an example for .NET:

  \b  (?<Area>  \d\d\d\)-  (?<Exch>  \d\d\d  )  -  (?<Num>  \d\d\d\d  )  \b  

and for Python/PHP:

  \b  (?P<Area>  \d\d\d\)-  (?P<Exch>  \d\d\d)-  (?P<Num>  \d\d\d\d)\b  

This "fills the names" Area , Exch , and Num with the components of a US phone number. The program can then refer to each matched substring through its name, for example, RegexObj.Groups ("Area") in VB.NET and most other .NET languages, RegexObj.Groups ["Area"] in C#, RegexObj . group("Area") in Python, and $matches ["Area"] in PHP. The result is clearer code.

Within the regular expression itself, the captured text is available via \k<Area> with .NET, and (?P=Area) in Python and PHP.

With Python and .NET (but not with PHP), you can use the same name more than once within the same expression. For example, to match the area code part of a US phone number, which look like ' (###) ' or ' ###- ', you might use (shown in .NET syntax): (?: \((?<Area>\d\d\d)\)(?<Area>\d\d\d)- ) . When either set matches, the three-digit code is saved to the name Area . Atomic grouping: (?>‹)

Atomic grouping, (?> ) , will be very easy to explain once the important details of how the regex engine carries out its work is understood (˜169). Here, Ill just say that once the parenthesized subexpression matches, what it matches is fixed (becomes atomic, unchangeable) for the rest of the match, unless it turns out that the whole set of atomic parentheses needs to be abandoned and subsequently revisited. A simple example helps to illustrate this indivisible, "atomic" nature of text matched by these parentheses.

The string ' Hola! ' is matched by .*! , but is not matched if .* is wrapped with atomic grouping, (?>.*)! . In either case, .* first internally matches as much as it can (' '), but the inability of the subsequent ! to match wants to force the .* to give up some of what it had matched (the final '!'). That cant happen in the second case because .* is inside atomic grouping, which never "gives up anything once the matching leaves them.

Although this example doesn't hint at it, atomic grouping has important uses. In particular, it can help make matching more efficient (˜171), and can be used to finely control what can and can't be matched (˜269). Alternation: ‹ ‹ ‹

Alternation allows several subexpressions to be tested at a given point. Each subexpression is called an alternative . The symbol is called various things, but or and bar seem popular. Some flavors use \ instead.

Alternation has very low precedence, so this andor that matches the same as (this and)(or that) , and not this (andor) that , even though visually, the andor looks like a unit.

Most flavors allow an empty alternative, as in (thisthat ) . The empty subexpression can always match, so this example is comparable to (thisthat)? . []

[] To be pedantic, (thisthat) is logically comparable to ((?:thisthat)?) . The minor difference from (thisthat)? is that a " nothingness match is not within the parentheses, a subtle difference important to tools that differentiate between a match of nothing and non-participation in the match.

The POSIX standard disallows an empty alternative, as does lex and most versions of awk. I think it's useful for its notational convenience or clarity. As Larry Wall told me once, "It's like having a zero in your numbering system." Conditional: (?if then else)

This construct allows you to express an if/then/else within a regex. The if part is a special kind of conditional expression discussed in a moment. Both the then and else parts are normal regex subexpressions. If the if part tests true, the then expression is attempted. Otherwise, the else part is attempted. (The else part may be omitted, and if so, the ' ' before it may be omitted as well.)

The kinds of if tests available are flavor-dependent, but most implementations allow at least special references to capturing subexpressions and lookaround.

Using a special reference to capturing parentheses as the test . If the if part is a number in parentheses, it evaluates to "true" if that numbered set of capturing parentheses has participated in the match to this point. Here's an example that matches an <IMG> HTML tag, either alone, or surrounded by <A>‹</A> link tags. It's shown in a free-spacing mode with comments, and the conditional construct (which in this example has no else part) is bold:

 (<A\s+[^>]+> \s*)? #  Match leading <A> tag, if there  .     <IMG\s+[^>]+>        #  Match <IMG> tag  .     (?(1)\s*</A>)           #  Match a closing </A>, if we'd matched an <A> before  . 

The (1) in (? (1) ‹) tests whether the first set of capturing parentheses participated in the match. "Participating in the match is very different from "actually matched some text," as a simple example illustrates...

Consider these two approaches to matching a word optionally wrapped in "<‹>": ( < )? \w+ (?(1) > ) works, but ( <? ) \w+ (?(1) > ) does not. The only difference between them is the location of the first question mark. In the first (correct) approach, the question mark governs the capturing parentheses, so the parentheses (and all they contain) are optional. In the flawed second approach, the capturing parentheses are not optional only the < matched within them is, so they "participate in the match" regardless of a '<' being matched or not. This means that the if part of (?(1)‹) always tests "true."

If named capture (˜138) is supported, you can generally use the name in parentheses instead of the number.

Using lookaround as the test . A full lookaround construct, such as (?=‹) and (?<=‹) , can be used as the if test. If the lookaround matches, it evaluates to "true," and so the then part is attempted. Otherwise, the else part is attempted. A somewhat contrived example that illustrates this is (? \d+\w+) , which attempts \d+ at positions just after NUM: , but attempts \w+ at other positions. The lookbehind conditional is underlined .

Other tests for the conditional . Perl adds an interesting twist to this conditional construct by allowing arbitrary Perl code to be executed as the test. The return value of the code is the test's value, indicating whether the then or else part should be attempted. This is covered in Chapter 7, on page 327. Greedy quantifiers: *, +, ?, {num,num}

The quantifiers (star, plus, question mark, and intervalsmetacharacters that affect the quantity of what they govern ) have already been discussed extensively. However, note that in some tools, \+ and \? are used instead of + and ? . Also, with some older tools, quantifiers cant be applied to a backreference or to a set of parentheses. Intervals {min,max}or \{min,max \}

Intervals can be considered a "counting quantifier" because you specify exactly the minimum number of matches you wish to require , and the maximum number of matches you wish to allow . If only a single number is given (such as in [a-z]{3} or [a-z]\{3\} , depending upon the flavor), it matches exactly that many of the item. This example is the same as [a-z][a-z][a-z] (although one may be more or less efficient than the other ˜251).

One caution: don't think you can use something like X{0,0} to mean "there must not be an X here." X{0,0} is a meaningless expression because it means " no requirement to match X , and, in fact, dont even bother trying to match any. Period. " It's the same as if the whole X{0,0} wasnt there at allif there is an X present, it could still be matched by something later in the expression, so your intended purpose is defeated. [ ] Use negative lookahead for a true "must not be here construct.

[ ] In theory, what I say about {0,0} is correct. In practice, what actually happens is even worse its almost random! In many programs (including GNU awk, GNU grep , and older versions of Perl) it seems that {0,0} means the same as *, while in many others (including most versions of sed that I've seen, and some versions of grep ) it means the same as ?. Crazy! Lazy quantifiers: *?, +?, ??, {num,num}?

Some tools offer the rather ungainly looking *?, +?, ??, and { min,max }?. These are the lazy versions of the quantifiers. (They are also called minimal matching , nongreedy , and ungreedy .) Quantifiers are normally "greedy," and try to match as much as possible. Conversely, these non-greedy versions match as little as possible, just the bare minimum needed to satisfy the match. The difference has far-reaching implications, covered in detail in the next chapter (˜159). Possessive quantifiers: *+, ++, ?+, {num,num}+

Currently supported only by java.util.regex and PCRE (and hence PHP), but likely to gain popularity, possessive quantifiers are like normally greedy quantifiers, but once they match something, they never "give it up." Like the atomic grouping to which they're related, understanding possessive quantifiers is much easier once the underlying match process is understood (which is the subject of the next chapter).

In one sense, possessive quantifiers are just syntactic sugar, as they can be mimicked with atomic grouping. Something like .++ has exactly the same result as (?> .+ ) , although a smart implementation can optimize possessive quantifiers more than atomic grouping (˜250).

Mastering Regular Expressions
Mastering Regular Expressions
ISBN: 0596528124
EAN: 2147483647
Year: 2004
Pages: 113

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: