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