XML technologies / Regular expressions / Extended introductory examples / Pattern matching with regular expressions / Recap

(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)

Although this chapter focused mainly on Perl, several points about regular expressions deserve a recap:

  1. Most tools have their own regular-expression dialect. Perl’s regular expressions appear to belong to the same general family as those of egrep, but Perl has a much richer set of metacharacters. Many programming languages, such as Java, Python, the .NET languages and Tcl use Perl-inspired dialects.

  2. In Perl, a regex is applied to a string with the construct $variable =~ m/.../. The m denotes the match operator; the slashes delimit the regular expression but are not part of it. The operator returns true or false.

  3. Metacharacters—characters interpreted specially—are not exclusive to regular expressions. As discussed for quoted strings in shells, several interpreters may compete to interpret the same characters. Knowing the metacharacters of each context (shell, regex, string, and others) matters when using Perl, PHP, Java, Tcl, GNU Emacs, awk, Python, or other modern scripting languages. (Within regular expressions, character classes naturally have their own set of metacharacters.)

  4. Perl and most of the languages mentioned provide the following regex abbreviations, among others (some have not yet been introduced):

    \t  A tab character
    \n  A newline
    \r  Carriage return
    \s  whitespace, whitespace characters (space, tab, newline, form feed, and so on)
    \S  Any character not included in ˹\s˼
    \w  ˹[a­zA­Z0­9_]˼ (a useful reminder is ˹\w+˼; w for “word”)
    \W Any character not included in ˹\w˼, that is, ˹[^a­zA­Z0­9_]˼
    \d ˹[0­9]˼, that is, a digit
    \D Any character not included in ˹\d˼, that is, ˹[^0­9]˼

  5. With the /i modifier⟦U+0002⟧, uppercase and lowercase letters are treated as equivalent. Although usually written “/i”, only the “i” is appended to the closing delimiter.

  6. The somewhat unsightly noncapturing parentheses ˹(?:...)˼ group parts of a regex without capturing and saving the matched text.

  7. After a successful match, Perl sets the variables $1, $2, $3 and so on, containing the text matched by subexpressions enclosed in ˹(...)˼. These variables let a regex extract individual portions ⟦U+0003⟧ of a string. (Other languages use the same principle differently; Features and dialects contains many examples.)

    Subexpressions are numbered from left to right by their opening parentheses, starting at 1. They may be nested, as in ˹(Vereinigte●Staaten(●von●Amerika)?)˼. Even when parentheses serve other purposes, such as forming groups for quantifiers, the numbered variables $1 and so on are still set.

  

 

 

 

German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.

O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln

back to top