3.5. Common Metacharacters and FeaturesThe 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.
3.5.1. Character RepresentationsThis group of metacharacters provides visually pleasing ways to match specific characters that are otherwise difficult to represent. 3.5.1.1. Character shorthandsMany utilities provide metacharacters to represent certain control characters that are sometimes machine-dependent , and which would otherwise be difficult to input or to visualize:
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. 3.5.1.2. These are machine dependent? As noted in the list, \n and \r are operating-system dependent in many tools, [
Table 3-6. A Few Utilities and Some of the Shorthand Metacharacters They Provide3.5.1.3. 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, Since awk does support octal escapes, you can use the ASCII code for the escape character directly: Table 3-7 on the next page shows the octal escapes some tools support. Some implementations, as a special case, allow 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. 3.5.1.4. 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, 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. 3.5.1.5. Control characters: \cchar Many flavors offer the 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 Table 3-7. A Few Utilities and the Octal and Hex Regex Escapes Their Regexes Support
3.5.2. Character Classes and Class-Like ConstructsModern 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. 3.5.2.1. 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, 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 Be sure to understand the underlying character set when using ranges. For example, 3.5.2.2. Almost any character: dotIn 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:
3.5.2.2.1. 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 3.5.2.3. Exactly one bytePerl 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. 3.5.2.4. Unicode combining character sequence: \X Perl and PHP support \X as a shorthand for 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 ' If you wanted to match either "francais" or "fran ais," it wouldn't be safe to just use 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 3.5.2.5. Class shorthands: \w, \d, \s, \W, \D, \SSupport for the following shorthands is common:
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. 3.5.2.6. 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 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 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
Additionally, some implementations support a special composite sub-property, \p{L&} , which is a shorthand for all "cased" letters, and is the same as 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 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 Table 3-9. Basic Unicode Sub-Properties
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:
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 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 Table 3-10. Property/Script/Block Features
3.5.2.7. 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 As another example, 3.5.2.8. 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 [ This brings us back to the 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,
(?!\p{Cn})\p{InThai} (?=\P{Cn})\p{InThai} \p{InThai}(?<!\p{Cn}) \p{InThai}(?<=\P{Cn}) 3.5.2.9. 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 [
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 The exact list of POSIX character classes is locale dependent, but the following are usually supported:
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. 3.5.2.10. 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 A collating sequence element is included within a bracket expression using a[.‹.] notation: 3.5.2.11. 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: 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, 3.5.2.12. Emacs syntax classes GNU Emacs doesn't support the traditional
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. 3.5.3.1. Start of line/string: ^, \A Caret When supported, 3.5.3.2. 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 . Two other common meanings for A match mode (˜112) can change the meaning of When supported, Table 3-11. Line Anchors for Some Scripting Languages
3.5.3.3. Start of match (or end of previous match): \G If a match is not successful, the location at which Perl's
See the sidebar on the next page for an example of these features in action. Despite these convenient features, Perl's 3.5.3.3.1. End of previous match, or start of the current match? One detail that differs among implementations is whether 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 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
3.5.3.4. 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 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." 3.5.3.5. 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 Table 3-12. A Few Utilities and Their Word Boundary Metacharacters
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 ModifiersWith 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. 3.5.4.1. 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 This example works with most systems that support
With most implementations except Python, the effects of 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
3.5.4.2. 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 3.5.4.3. Comments: (?#‹) and #‹ Some flavors support comments via 3.5.4.4. Literal-text span: \Q‹\EFirst 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). 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 The java.util.regex support for 3.5.5. Grouping, Capturing, Conditionals, and Control3.5.5.1. Capturing/Grouping Parentheses: (‹) and \1, \2, ... Common, unadorned parentheses generally perform two functions, grouping and capturing. Common parentheses are almost always of the form 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 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 3.5.5.2. Grouping-only parentheses: (?:‹) Grouping-only 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
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. 3.5.5.3. Named capture: (?<Name>‹) Python, PHP's preg engine, and .NET languages support captures to named locations. Python and PHP use the syntax
and for Python/PHP:
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 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): 3.5.5.4. Atomic grouping: (?>‹) Atomic grouping, The string ' Hola! ' is matched by 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). 3.5.5.5. Alternation: ‹ ‹ ‹ Alternation allows several subexpressions to be tested at a given point. Each subexpression is called an alternative . The Alternation has very low precedence, so Most flavors allow an empty alternative, as in
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." 3.5.5.6. 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 Consider these two approaches to matching a word optionally wrapped in "<‹>": 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 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. 3.5.5.7. 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, 3.5.5.7.1. 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 One caution: don't think you can use something like
3.5.5.8. 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). 3.5.5.9. 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 |