home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / RiscOS / APP / DEVS / PERL / RPC106.ZIP / Rpc106 / Docs / perlre < prev    next >
Text File  |  1997-11-23  |  25KB  |  605 lines

  1. NAME
  2.     perlre - Perl regular expressions
  3.  
  4. DESCRIPTION
  5.     This page describes the syntax of regular expressions in Perl.
  6.     For a description of how to *use* regular expressions in
  7.     matching operations, plus various examples of the same, see
  8.     `m//' and `s///' in the perlop manpage.
  9.  
  10.     The matching operations can have various modifiers. The
  11.     modifiers which relate to the interpretation of the regular
  12.     expression inside are listed below. For the modifiers that alter
  13.     the behaviour of the operation, see the section on "m//" in the
  14.     perlop manpage and the section on "s//" in the perlop manpage.
  15.  
  16.     i    Do case-insensitive pattern matching.
  17.  
  18.     If `use locale' is in effect, the case map is taken from the
  19.     current locale. See the perllocale manpage.
  20.  
  21.     m    Treat string as multiple lines. That is, change "^" and "$" from
  22.     matching at only the very start or end of the string to the
  23.     start or end of any line anywhere within the string,
  24.  
  25.     s    Treat string as single line. That is, change "." to match any
  26.     character whatsoever, even a newline, which it normally
  27.     would not match.
  28.  
  29.     x    Extend your pattern's legibility by permitting whitespace and
  30.     comments.
  31.  
  32.     These are usually written as "the `/x' modifier", even though
  33.     the delimiter in question might not actually be a slash. In
  34.     fact, any of these modifiers may also be embedded within the
  35.     regular expression itself using the new `(?...)' construct. See
  36.     below.
  37.  
  38.     The `/x' modifier itself needs a little more explanation. It
  39.     tells the regular expression parser to ignore whitespace that is
  40.     neither backslashed nor within a character class. You can use
  41.     this to break up your regular expression into (slightly) more
  42.     readable parts. The `#' character is also treated as a
  43.     metacharacter introducing a comment, just as in ordinary Perl
  44.     code. This also means that if you want real whitespace or `#'
  45.     characters in the pattern that you'll have to either escape them
  46.     or encode them using octal or hex escapes. Taken together, these
  47.     features go a long way towards making Perl's regular expressions
  48.     more readable. See the C comment deletion code in the perlop
  49.     manpage.
  50.  
  51.   Regular Expressions
  52.  
  53.     The patterns used in pattern matching are regular expressions
  54.     such as those supplied in the Version 8 regexp routines. (In
  55.     fact, the routines are derived (distantly) from Henry Spencer's
  56.     freely redistributable reimplementation of the V8 routines.) See
  57.     the section on "Version 8 Regular Expressions" for details.
  58.  
  59.     In particular the following metacharacters have their standard
  60.     *egrep*-ish meanings:
  61.  
  62.     \   Quote the next metacharacter
  63.     ^   Match the beginning of the line
  64.     .   Match any character (except newline)
  65.     $   Match the end of the line (or before newline at the end)
  66.     |   Alternation
  67.     ()  Grouping
  68.     []  Character class
  69.  
  70.     By default, the "^" character is guaranteed to match at only the
  71.     beginning of the string, the "$" character at only the end (or
  72.     before the newline at the end) and Perl does certain
  73.     optimizations with the assumption that the string contains only
  74.     one line. Embedded newlines will not be matched by "^" or "$".
  75.     You may, however, wish to treat a string as a multi-line buffer,
  76.     such that the "^" will match after any newline within the
  77.     string, and "$" will match before any newline. At the cost of a
  78.     little more overhead, you can do this by using the /m modifier
  79.     on the pattern match operator. (Older programs did this by
  80.     setting `$*', but this practice is now deprecated.)
  81.  
  82.     To facilitate multi-line substitutions, the "." character never
  83.     matches a newline unless you use the `/s' modifier, which in
  84.     effect tells Perl to pretend the string is a single line--even
  85.     if it isn't. The `/s' modifier also overrides the setting of
  86.     `$*', in case you have some (badly behaved) older code that sets
  87.     it in another module.
  88.  
  89.     The following standard quantifiers are recognized:
  90.  
  91.     *      Match 0 or more times
  92.     +      Match 1 or more times
  93.     ?      Match 1 or 0 times
  94.     {n}    Match exactly n times
  95.     {n,}   Match at least n times
  96.     {n,m}  Match at least n but not more than m times
  97.  
  98.     (If a curly bracket occurs in any other context, it is treated
  99.     as a regular character.) The "*" modifier is equivalent to
  100.     `{0,}', the "+" modifier to `{1,}', and the "?" modifier to
  101.     `{0,1}'. n and m are limited to integral values less than 65536.
  102.  
  103.     By default, a quantified subpattern is "greedy", that is, it
  104.     will match as many times as possible (given a particular
  105.     starting location) while still allowing the rest of the pattern
  106.     to match. If you want it to match the minimum number of times
  107.     possible, follow the quantifier with a "?". Note that the
  108.     meanings don't change, just the "greediness":
  109.  
  110.     *?     Match 0 or more times
  111.     +?     Match 1 or more times
  112.     ??     Match 0 or 1 time
  113.     {n}?   Match exactly n times
  114.     {n,}?  Match at least n times
  115.     {n,m}? Match at least n but not more than m times
  116.  
  117.     Because patterns are processed as double quoted strings, the
  118.     following also work:
  119.  
  120.     \t        tab           (HT, TAB)
  121.     \n        newline          (LF, NL)
  122.     \r        return          (CR)
  123.     \f        form feed          (FF)
  124.     \a        alarm (bell)      (BEL)
  125.     \e        escape (think troff)  (ESC)
  126.     \033        octal char (think of a PDP-11)
  127.     \x1B        hex char
  128.     \c[        control char
  129.     \l        lowercase next char (think vi)
  130.     \u        uppercase next char (think vi)
  131.     \L        lowercase till \E (think vi)
  132.     \U        uppercase till \E (think vi)
  133.     \E        end case modification (think vi)
  134.     \Q        quote (disable) regexp metacharacters till \E
  135.  
  136.     If `use locale' is in effect, the case map used by `\l', `\L',
  137.     `\u' and <\U> is taken from the current locale. See the
  138.     perllocale manpage.
  139.  
  140.     In addition, Perl defines the following:
  141.  
  142.     \w  Match a "word" character (alphanumeric plus "_")
  143.     \W  Match a non-word character
  144.     \s  Match a whitespace character
  145.     \S  Match a non-whitespace character
  146.     \d  Match a digit character
  147.     \D  Match a non-digit character
  148.  
  149.     Note that `\w' matches a single alphanumeric character, not a
  150.     whole word. To match a word you'd need to say `\w+'. If `use
  151.     locale' is in effect, the list of alphabetic characters
  152.     generated by `\w' is taken from the current locale. See the
  153.     perllocale manpage. You may use `\w', `\W', `\s', `\S', `\d',
  154.     and `\D' within character classes (though not as either end of a
  155.     range).
  156.  
  157.     Perl defines the following zero-width assertions:
  158.  
  159.     \b  Match a word boundary
  160.     \B  Match a non-(word boundary)
  161.     \A  Match at only beginning of string
  162.     \Z  Match at only end of string (or before newline at the end)
  163.     \G  Match only where previous m//g left off (works only with /g)
  164.  
  165.     A word boundary (`\b') is defined as a spot between two
  166.     characters that has a `\w' on one side of it and a `\W' on the
  167.     other side of it (in either order), counting the imaginary
  168.     characters off the beginning and end of the string as matching a
  169.     `\W'. (Within character classes `\b' represents backspace rather
  170.     than a word boundary.) The `\A' and `\Z' are just like "^" and
  171.     "$" except that they won't match multiple times when the `/m'
  172.     modifier is used, while "^" and "$" will match at every internal
  173.     line boundary. To match the actual end of the string, not
  174.     ignoring newline, you can use `\Z(?!\n)'. The `\G' assertion can
  175.     be used to chain global matches (using `m//g'), as described in
  176.     the section on "Regexp Quote-Like Operators" in the perlop
  177.     manpage.
  178.  
  179.     It is also useful when writing `lex'-like scanners, when you
  180.     have several regexps which you want to match against consequent
  181.     substrings of your string, see the previous reference. The
  182.     actual location where `\G' will match can also be influenced by
  183.     using `pos()' as an lvalue. See the "pos" entry in the perlfunc
  184.     manpage.
  185.  
  186.     When the bracketing construct `( ... )' is used, \<digit>
  187.     matches the digit'th substring. Outside of the pattern, always
  188.     use "$" instead of "\" in front of the digit. (While the
  189.     \<digit> notation can on rare occasion work outside the current
  190.     pattern, this should not be relied upon. See the WARNING below.)
  191.     The scope of $<digit> (and `$`', `$&', and `$'') extends to the
  192.     end of the enclosing BLOCK or eval string, or to the next
  193.     successful pattern match, whichever comes first. If you want to
  194.     use parentheses to delimit a subpattern (e.g., a set of
  195.     alternatives) without saving it as a subpattern, follow the (
  196.     with a ?:.
  197.  
  198.     You may have as many parentheses as you wish. If you have more
  199.     than 9 substrings, the variables $10, $11, ... refer to the
  200.     corresponding substring. Within the pattern, \10, \11, etc.
  201.     refer back to substrings if there have been at least that many
  202.     left parentheses before the backreference. Otherwise (for
  203.     backward compatibility) \10 is the same as \010, a backspace,
  204.     and \11 the same as \011, a tab. And so on. (\1 through \9 are
  205.     always backreferences.)
  206.  
  207.     `$+' returns whatever the last bracket match matched. `$&'
  208.     returns the entire matched string. (`$0' used to return the same
  209.     thing, but not any more.) `$`' returns everything before the
  210.     matched string. `$'' returns everything after the matched
  211.     string. Examples:
  212.  
  213.     s/^([^ ]*) *([^ ]*)/$2 $1/;    # swap first two words
  214.  
  215.     if (/Time: (..):(..):(..)/) {
  216.         $hours = $1;
  217.         $minutes = $2;
  218.         $seconds = $3;
  219.     }
  220.  
  221.     Once perl sees that you need one of `$&', `$`' or `$'' anywhere
  222.     in the program, it has to provide them on each and every pattern
  223.     match. This can slow your program down. The same mechanism that
  224.     handles these provides for the use of $1, $2, etc., so you pay
  225.     the same price for each regexp that contains capturing
  226.     parentheses. But if you never use $&, etc., in your script, then
  227.     regexps *without* capturing parentheses won't be penalized. So
  228.     avoid $&, $', and $` if you can, but if you can't (and some
  229.     algorithms really appreciate them), once you've used them once,
  230.     use them at will, because you've already paid the price.
  231.  
  232.     You will note that all backslashed metacharacters in Perl are
  233.     alphanumeric, such as `\b', `\w', `\n'. Unlike some other
  234.     regular expression languages, there are no backslashed symbols
  235.     that aren't alphanumeric. So anything that looks like \\, \(,
  236.     \), \<, \>, \{, or \} is always interpreted as a literal
  237.     character, not a metacharacter. This was once used in a common
  238.     idiom to disable or quote the special meanings of regular
  239.     expression metacharacters in a string that you want to use for a
  240.     pattern. Simply quote all the non-alphanumeric characters:
  241.  
  242.     $pattern =~ s/(\W)/\\$1/g;
  243.  
  244.     Now it is much more common to see either the quotemeta()
  245.     function or the \Q escape sequence used to disable the
  246.     metacharacters special meanings like this:
  247.  
  248.     /$unquoted\Q$quoted\E$unquoted/
  249.  
  250.     Perl defines a consistent extension syntax for regular
  251.     expressions. The syntax is a pair of parentheses with a question
  252.     mark as the first thing within the parentheses (this was a
  253.     syntax error in older versions of Perl). The character after the
  254.     question mark gives the function of the extension. Several
  255.     extensions are already supported:
  256.  
  257.     (?#text)  A comment. The text is ignored. If the `/x' switch is used
  258.           to enable whitespace formatting, a simple `#' will
  259.           suffice.
  260.  
  261.     (?:regexp)
  262.           This groups things like "()" but doesn't make
  263.           backreferences like "()" does. So
  264.  
  265.           split(/\b(?:a|b|c)\b/)
  266.  
  267.           is like
  268.  
  269.           split(/\b(a|b|c)\b/)
  270.  
  271.           but doesn't spit out extra fields.
  272.  
  273.     (?=regexp)
  274.           A zero-width positive lookahead assertion. For
  275.           example, `/\w+(?=\t)/' matches a word followed by a
  276.           tab, without including the tab in `$&'.
  277.  
  278.     (?!regexp)
  279.           A zero-width negative lookahead assertion. For example
  280.           `/foo(?!bar)/' matches any occurrence of "foo" that
  281.           isn't followed by "bar". Note however that lookahead
  282.           and lookbehind are NOT the same thing. You cannot use
  283.           this for lookbehind: `/(?!foo)bar/' will not find an
  284.           occurrence of "bar" that is preceded by something
  285.           which is not "foo". That's because the `(?!foo)' is
  286.           just saying that the next thing cannot be "foo"--and
  287.           it's not, it's a "bar", so "foobar" will match. You
  288.           would have to do something like `/(?!foo)...bar/' for
  289.           that. We say "like" because there's the case of your
  290.           "bar" not having three characters before it. You could
  291.           cover that this way: `/(?:(?!foo)...|^..?)bar/'.
  292.           Sometimes it's still easier just to say:
  293.  
  294.           if (/foo/ && $` =~ /bar$/)
  295.  
  296.     (?imsx)   One or more embedded pattern-match modifiers. This is
  297.           particularly useful for patterns that are specified in
  298.           a table somewhere, some of which want to be case
  299.           sensitive, and some of which don't. The case
  300.           insensitive ones need to include merely `(?i)' at the
  301.           front of the pattern. For example:
  302.  
  303.           $pattern = "foobar";
  304.           if ( /$pattern/i )
  305.  
  306.           # more flexible:
  307.  
  308.           $pattern = "(?i)foobar";
  309.           if ( /$pattern/ )
  310.  
  311.     The specific choice of question mark for this and the new
  312.     minimal matching construct was because 1) question mark is
  313.     pretty rare in older regular expressions, and 2) whenever you
  314.     see one, you should stop and "question" exactly what is going
  315.     on. That's psychology...
  316.  
  317.   Backtracking
  318.  
  319.     A fundamental feature of regular expression matching involves
  320.     the notion called *backtracking*. which is used (when needed) by
  321.     all regular expression quantifiers, namely `*', `*?', `+', `+?',
  322.     `{n,m}', and `{n,m}?'.
  323.  
  324.     For a regular expression to match, the *entire* regular
  325.     expression must match, not just part of it. So if the beginning
  326.     of a pattern containing a quantifier succeeds in a way that
  327.     causes later parts in the pattern to fail, the matching engine
  328.     backs up and recalculates the beginning part--that's why it's
  329.     called backtracking.
  330.  
  331.     Here is an example of backtracking: Let's say you want to find
  332.     the word following "foo" in the string "Food is on the foo
  333.     table.":
  334.  
  335.     $_ = "Food is on the foo table.";
  336.     if ( /\b(foo)\s+(\w+)/i ) {
  337.         print "$2 follows $1.\n";
  338.     }
  339.  
  340.     When the match runs, the first part of the regular expression
  341.     (`\b(foo)') finds a possible match right at the beginning of the
  342.     string, and loads up $1 with "Foo". However, as soon as the
  343.     matching engine sees that there's no whitespace following the
  344.     "Foo" that it had saved in $1, it realizes its mistake and
  345.     starts over again one character after where it had the tentative
  346.     match. This time it goes all the way until the next occurrence
  347.     of "foo". The complete regular expression matches this time, and
  348.     you get the expected output of "table follows foo."
  349.  
  350.     Sometimes minimal matching can help a lot. Imagine you'd like to
  351.     match everything between "foo" and "bar". Initially, you write
  352.     something like this:
  353.  
  354.     $_ =  "The food is under the bar in the barn.";
  355.     if ( /foo(.*)bar/ ) {
  356.         print "got <$1>\n";
  357.     }
  358.  
  359.     Which perhaps unexpectedly yields:
  360.  
  361.       got <d is under the bar in the >
  362.  
  363.     That's because `.*' was greedy, so you get everything between
  364.     the *first* "foo" and the *last* "bar". In this case, it's more
  365.     effective to use minimal matching to make sure you get the text
  366.     between a "foo" and the first "bar" thereafter.
  367.  
  368.     if ( /foo(.*?)bar/ ) { print "got <$1>\n" }
  369.       got <d is under the >
  370.  
  371.     Here's another example: let's say you'd like to match a number
  372.     at the end of a string, and you also want to keep the preceding
  373.     part the match. So you write this:
  374.  
  375.     $_ = "I have 2 numbers: 53147";
  376.     if ( /(.*)(\d*)/ ) {                    # Wrong!
  377.         print "Beginning is <$1>, number is <$2>.\n";
  378.     }
  379.  
  380.     That won't work at all, because `.*' was greedy and gobbled up
  381.     the whole string. As `\d*' can match on an empty string the
  382.     complete regular expression matched successfully.
  383.  
  384.     Beginning is <I have 2 numbers: 53147>, number is <>.
  385.  
  386.     Here are some variants, most of which don't work:
  387.  
  388.     $_ = "I have 2 numbers: 53147";
  389.     @pats = qw{
  390.         (.*)(\d*)
  391.         (.*)(\d+)
  392.         (.*?)(\d*)
  393.         (.*?)(\d+)
  394.         (.*)(\d+)$
  395.         (.*?)(\d+)$
  396.         (.*)\b(\d+)$
  397.         (.*\D)(\d+)$
  398.     };
  399.  
  400.     for $pat (@pats) {
  401.         printf "%-12s ", $pat;
  402.         if ( /$pat/ ) {
  403.         print "<$1> <$2>\n";
  404.         } else {
  405.         print "FAIL\n";
  406.         }
  407.     }
  408.  
  409.     That will print out:
  410.  
  411.     (.*)(\d*)    <I have 2 numbers: 53147> <>
  412.     (.*)(\d+)    <I have 2 numbers: 5314> <7>
  413.     (.*?)(\d*)   <> <>
  414.     (.*?)(\d+)   <I have > <2>
  415.     (.*)(\d+)$   <I have 2 numbers: 5314> <7>
  416.     (.*?)(\d+)$  <I have 2 numbers: > <53147>
  417.     (.*)\b(\d+)$ <I have 2 numbers: > <53147>
  418.     (.*\D)(\d+)$ <I have 2 numbers: > <53147>
  419.  
  420.     As you see, this can be a bit tricky. It's important to realize
  421.     that a regular expression is merely a set of assertions that
  422.     gives a definition of success. There may be 0, 1, or several
  423.     different ways that the definition might succeed against a
  424.     particular string. And if there are multiple ways it might
  425.     succeed, you need to understand backtracking to know which
  426.     variety of success you will achieve.
  427.  
  428.     When using lookahead assertions and negations, this can all get
  429.     even tricker. Imagine you'd like to find a sequence of non-
  430.     digits not followed by "123". You might try to write that as
  431.  
  432.         $_ = "ABC123";
  433.         if ( /^\D*(?!123)/ ) {                # Wrong!
  434.         print "Yup, no 123 in $_\n";
  435.         }
  436.  
  437.     But that isn't going to match; at least, not the way you're
  438.     hoping. It claims that there is no 123 in the string. Here's a
  439.     clearer picture of why it that pattern matches, contrary to
  440.     popular expectations:
  441.  
  442.     $x = 'ABC123' ;
  443.     $y = 'ABC445' ;
  444.  
  445.     print "1: got $1\n" if $x =~ /^(ABC)(?!123)/ ;
  446.     print "2: got $1\n" if $y =~ /^(ABC)(?!123)/ ;
  447.  
  448.     print "3: got $1\n" if $x =~ /^(\D*)(?!123)/ ;
  449.     print "4: got $1\n" if $y =~ /^(\D*)(?!123)/ ;
  450.  
  451.     This prints
  452.  
  453.     2: got ABC
  454.     3: got AB
  455.     4: got ABC
  456.  
  457.     You might have expected test 3 to fail because it seems to a
  458.     more general purpose version of test 1. The important difference
  459.     between them is that test 3 contains a quantifier (`\D*') and so
  460.     can use backtracking, whereas test 1 will not. What's happening
  461.     is that you've asked "Is it true that at the start of $x,
  462.     following 0 or more non-digits, you have something that's not
  463.     123?" If the pattern matcher had let `\D*' expand to "ABC", this
  464.     would have caused the whole pattern to fail. The search engine
  465.     will initially match `\D*' with "ABC". Then it will try to match
  466.     `(?!123' with "123" which, of course, fails. But because a
  467.     quantifier (`\D*') has been used in the regular expression, the
  468.     search engine can backtrack and retry the match differently in
  469.     the hope of matching the complete regular expression.
  470.  
  471.     Well now, the pattern really, *really* wants to succeed, so it
  472.     uses the standard regexp back-off-and-retry and lets `\D*'
  473.     expand to just "AB" this time. Now there's indeed something
  474.     following "AB" that is not "123". It's in fact "C123", which
  475.     suffices.
  476.  
  477.     We can deal with this by using both an assertion and a negation.
  478.     We'll say that the first part in $1 must be followed by a digit,
  479.     and in fact, it must also be followed by something that's not
  480.     "123". Remember that the lookaheads are zero-width expressions--
  481.     they only look, but don't consume any of the string in their
  482.     match. So rewriting this way produces what you'd expect; that
  483.     is, case 5 will fail, but case 6 succeeds:
  484.  
  485.     print "5: got $1\n" if $x =~ /^(\D*)(?=\d)(?!123)/ ;
  486.     print "6: got $1\n" if $y =~ /^(\D*)(?=\d)(?!123)/ ;
  487.  
  488.     6: got ABC
  489.  
  490.     In other words, the two zero-width assertions next to each other
  491.     work like they're ANDed together, just as you'd use any builtin
  492.     assertions: `/^$/' matches only if you're at the beginning of
  493.     the line AND the end of the line simultaneously. The deeper
  494.     underlying truth is that juxtaposition in regular expressions
  495.     always means AND, except when you write an explicit OR using the
  496.     vertical bar. `/ab/' means match "a" AND (then) match "b",
  497.     although the attempted matches are made at different positions
  498.     because "a" is not a zero-width assertion, but a one-width
  499.     assertion.
  500.  
  501.     One warning: particularly complicated regular expressions can
  502.     take exponential time to solve due to the immense number of
  503.     possible ways they can use backtracking to try match. For
  504.     example this will take a very long time to run
  505.  
  506.     /((a{0,5}){0,5}){0,5}/
  507.  
  508.     And if you used `*''s instead of limiting it to 0 through 5
  509.     matches, then it would take literally forever--or until you ran
  510.     out of stack space.
  511.  
  512.   Version 8 Regular Expressions
  513.  
  514.     In case you're not familiar with the "regular" Version 8 regexp
  515.     routines, here are the pattern-matching rules not described
  516.     above.
  517.  
  518.     Any single character matches itself, unless it is a
  519.     *metacharacter* with a special meaning described here or above.
  520.     You can cause characters which normally function as
  521.     metacharacters to be interpreted literally by prefixing them
  522.     with a "\" (e.g., "\." matches a ".", not any character; "\\"
  523.     matches a "\"). A series of characters matches that series of
  524.     characters in the target string, so the pattern `blurfl' would
  525.     match "blurfl" in the target string.
  526.  
  527.     You can specify a character class, by enclosing a list of
  528.     characters in `[]', which will match any one of the characters
  529.     in the list. If the first character after the "[" is "^", the
  530.     class matches any character not in the list. Within a list, the
  531.     "-" character is used to specify a range, so that `a-z'
  532.     represents all the characters between "a" and "z", inclusive. If
  533.     you want "-" itself to be a member of a class, put it at the
  534.     start or end of the list, or escape it with a backslash. (The
  535.     following all specify the same class of three characters: `[-
  536.     az]', `[az-]', and `[a\-z]'. All are different from `[a-z]',
  537.     which specifies a class containing twenty-six characters.)
  538.  
  539.     Characters may be specified using a metacharacter syntax much
  540.     like that used in C: "\n" matches a newline, "\t" a tab, "\r" a
  541.     carriage return, "\f" a form feed, etc. More generally, \*nnn*,
  542.     where *nnn* is a string of octal digits, matches the character
  543.     whose ASCII value is *nnn*. Similarly, \x*nn*, where *nn* are
  544.     hexadecimal digits, matches the character whose ASCII value is
  545.     *nn*. The expression \c*x* matches the ASCII character control-
  546.     *x*. Finally, the "." metacharacter matches any character except
  547.     "\n" (unless you use `/s').
  548.  
  549.     You can specify a series of alternatives for a pattern using "|"
  550.     to separate them, so that `fee|fie|foe' will match any of "fee",
  551.     "fie", or "foe" in the target string (as would `f(e|i|o)e').
  552.     Note that the first alternative includes everything from the
  553.     last pattern delimiter ("(", "[", or the beginning of the
  554.     pattern) up to the first "|", and the last alternative contains
  555.     everything from the last "|" to the next pattern delimiter. For
  556.     this reason, it's common practice to include alternatives in
  557.     parentheses, to minimize confusion about where they start and
  558.     end. Note however that "|" is interpreted as a literal with
  559.     square brackets, so if you write `[fee|fie|foe]' you're really
  560.     only matching `[feio|]'.
  561.  
  562.     Within a pattern, you may designate subpatterns for later
  563.     reference by enclosing them in parentheses, and you may refer
  564.     back to the *n*th subpattern later in the pattern using the
  565.     metacharacter \*n*. Subpatterns are numbered based on the left
  566.     to right order of their opening parenthesis. Note that a
  567.     backreference matches whatever actually matched the subpattern
  568.     in the string being examined, not the rules for that subpattern.
  569.     Therefore, `(0|0x)\d*\s\1\d*' will match "0x1234 0x4321",but not
  570.     "0x1234 01234", because subpattern 1 actually matched "0x", even
  571.     though the rule `0|0x' could potentially match the leading 0 in
  572.     the second number.
  573.  
  574.   WARNING on \1 vs $1
  575.  
  576.     Some people get too used to writing things like
  577.  
  578.     $pattern =~ s/(\W)/\\\1/g;
  579.  
  580.     This is grandfathered for the RHS of a substitute to avoid
  581.     shocking the sed addicts, but it's a dirty habit to get into.
  582.     That's because in PerlThink, the righthand side of a `s///' is a
  583.     double-quoted string. `\1' in the usual double-quoted string
  584.     means a control-A. The customary Unix meaning of `\1' is kludged
  585.     in for `s///'. However, if you get into the habit of doing that,
  586.     you get yourself into trouble if you then add an `/e' modifier.
  587.  
  588.     s/(\d+)/ \1 + 1 /eg;
  589.  
  590.     Or if you try to do
  591.  
  592.     s/(\d+)/\1000/;
  593.  
  594.     You can't disambiguate that by saying `\{1}000', whereas you can
  595.     fix it with `${1}000'. Basically, the operation of interpolation
  596.     should not be confused with the operation of matching a
  597.     backreference. Certainly they mean two different things on the
  598.     *left* side of the `s///'.
  599.  
  600.   SEE ALSO
  601.  
  602.     "Mastering Regular Expressions" (see the perlbook manpage) by
  603.     Jeffrey Friedl.
  604.  
  605.