This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Perl_sv_vcatpvfn_flags: get rid of an (int) cast
[perl5.git] / pod / perlretut.pod
1 =head1 NAME
2
3 perlretut - Perl regular expressions tutorial
4
5 =head1 DESCRIPTION
6
7 This page provides a basic tutorial on understanding, creating and
8 using regular expressions in Perl.  It serves as a complement to the
9 reference page on regular expressions L<perlre>.  Regular expressions
10 are an integral part of the C<m//>, C<s///>, C<qr//> and C<split>
11 operators and so this tutorial also overlaps with
12 L<perlop/"Regexp Quote-Like Operators"> and L<perlfunc/split>.
13
14 Perl is widely renowned for excellence in text processing, and regular
15 expressions are one of the big factors behind this fame.  Perl regular
16 expressions display an efficiency and flexibility unknown in most
17 other computer languages.  Mastering even the basics of regular
18 expressions will allow you to manipulate text with surprising ease.
19
20 What is a regular expression?  At its most basic, a regular expression
21 is a template that is used to determine if a string has certain
22 characteristics.  The string is most often some text, such as a line,
23 sentence, web page, or even a whole book, but less commonly it could be
24 some binary data as well.
25 Suppose we want to determine if the text in variable, C<$var> contains
26 the sequence of characters S<C<m u s h r o o m>>
27 (blanks added for legibility).  We can write in Perl
28
29  $var =~ m/mushroom/
30
31 The value of this expression will be TRUE if C<$var> contains that
32 sequence of characters, and FALSE otherwise.  The portion enclosed in
33 C<'E<sol>'> characters denotes the characteristic we are looking for.
34 We use the term I<pattern> for it.  The process of looking to see if the
35 pattern occurs in the string is called I<matching>, and the C<"=~">
36 operator along with the C<m//> tell Perl to try to match the pattern
37 against the string.  Note that the pattern is also a string, but a very
38 special kind of one, as we will see.  Patterns are in common use these
39 days;
40 examples are the patterns typed into a search engine to find web pages
41 and the patterns used to list files in a directory, I<e.g.>, "C<ls *.txt>"
42 or "C<dir *.*>".  In Perl, the patterns described by regular expressions
43 are used not only to search strings, but to also extract desired parts
44 of strings, and to do search and replace operations.
45
46 Regular expressions have the undeserved reputation of being abstract
47 and difficult to understand.  This really stems simply because the
48 notation used to express them tends to be terse and dense, and not
49 because of inherent complexity.  We recommend using the C</x> regular
50 expression modifier (described below) along with plenty of white space
51 to make them less dense, and easier to read.  Regular expressions are
52 constructed using
53 simple concepts like conditionals and loops and are no more difficult
54 to understand than the corresponding C<if> conditionals and C<while>
55 loops in the Perl language itself.
56
57 This tutorial flattens the learning curve by discussing regular
58 expression concepts, along with their notation, one at a time and with
59 many examples.  The first part of the tutorial will progress from the
60 simplest word searches to the basic regular expression concepts.  If
61 you master the first part, you will have all the tools needed to solve
62 about 98% of your needs.  The second part of the tutorial is for those
63 comfortable with the basics and hungry for more power tools.  It
64 discusses the more advanced regular expression operators and
65 introduces the latest cutting-edge innovations.
66
67 A note: to save time, "regular expression" is often abbreviated as
68 regexp or regex.  Regexp is a more natural abbreviation than regex, but
69 is harder to pronounce.  The Perl pod documentation is evenly split on
70 regexp vs regex; in Perl, there is more than one way to abbreviate it.
71 We'll use regexp in this tutorial.
72
73 New in v5.22, L<C<use re 'strict'>|re/'strict' mode> applies stricter
74 rules than otherwise when compiling regular expression patterns.  It can
75 find things that, while legal, may not be what you intended.
76
77 =head1 Part 1: The basics
78
79 =head2 Simple word matching
80
81 The simplest regexp is simply a word, or more generally, a string of
82 characters.  A regexp consisting of just a word matches any string that
83 contains that word:
84
85     "Hello World" =~ /World/;  # matches
86
87 What is this Perl statement all about? C<"Hello World"> is a simple
88 double-quoted string.  C<World> is the regular expression and the
89 C<//> enclosing C</World/> tells Perl to search a string for a match.
90 The operator C<=~> associates the string with the regexp match and
91 produces a true value if the regexp matched, or false if the regexp
92 did not match.  In our case, C<World> matches the second word in
93 C<"Hello World">, so the expression is true.  Expressions like this
94 are useful in conditionals:
95
96     if ("Hello World" =~ /World/) {
97         print "It matches\n";
98     }
99     else {
100         print "It doesn't match\n";
101     }
102
103 There are useful variations on this theme.  The sense of the match can
104 be reversed by using the C<!~> operator:
105
106     if ("Hello World" !~ /World/) {
107         print "It doesn't match\n";
108     }
109     else {
110         print "It matches\n";
111     }
112
113 The literal string in the regexp can be replaced by a variable:
114
115     my $greeting = "World";
116     if ("Hello World" =~ /$greeting/) {
117         print "It matches\n";
118     }
119     else {
120         print "It doesn't match\n";
121     }
122
123 If you're matching against the special default variable C<$_>, the
124 C<$_ =~> part can be omitted:
125
126     $_ = "Hello World";
127     if (/World/) {
128         print "It matches\n";
129     }
130     else {
131         print "It doesn't match\n";
132     }
133
134 And finally, the C<//> default delimiters for a match can be changed
135 to arbitrary delimiters by putting an C<'m'> out front:
136
137     "Hello World" =~ m!World!;   # matches, delimited by '!'
138     "Hello World" =~ m{World};   # matches, note the matching '{}'
139     "/usr/bin/perl" =~ m"/perl"; # matches after '/usr/bin',
140                                  # '/' becomes an ordinary char
141
142 C</World/>, C<m!World!>, and C<m{World}> all represent the
143 same thing.  When, I<e.g.>, the quote (C<'"'>) is used as a delimiter, the forward
144 slash C<'/'> becomes an ordinary character and can be used in this regexp
145 without trouble.
146
147 Let's consider how different regexps would match C<"Hello World">:
148
149     "Hello World" =~ /world/;  # doesn't match
150     "Hello World" =~ /o W/;    # matches
151     "Hello World" =~ /oW/;     # doesn't match
152     "Hello World" =~ /World /; # doesn't match
153
154 The first regexp C<world> doesn't match because regexps are
155 case-sensitive.  The second regexp matches because the substring
156 S<C<'o W'>> occurs in the string S<C<"Hello World">>.  The space
157 character C<' '> is treated like any other character in a regexp and is
158 needed to match in this case.  The lack of a space character is the
159 reason the third regexp C<'oW'> doesn't match.  The fourth regexp
160 "C<World >" doesn't match because there is a space at the end of the
161 regexp, but not at the end of the string.  The lesson here is that
162 regexps must match a part of the string I<exactly> in order for the
163 statement to be true.
164
165 If a regexp matches in more than one place in the string, Perl will
166 always match at the earliest possible point in the string:
167
168     "Hello World" =~ /o/;       # matches 'o' in 'Hello'
169     "That hat is red" =~ /hat/; # matches 'hat' in 'That'
170
171 With respect to character matching, there are a few more points you
172 need to know about.   First of all, not all characters can be used "as
173 is" in a match.  Some characters, called I<metacharacters>, are reserved
174 for use in regexp notation.  The metacharacters are
175
176     {}[]()^$.|*+?-\
177
178 The significance of each of these will be explained
179 in the rest of the tutorial, but for now, it is important only to know
180 that a metacharacter can be matched as-is by putting a backslash before
181 it:
182
183     "2+2=4" =~ /2+2/;    # doesn't match, + is a metacharacter
184     "2+2=4" =~ /2\+2/;   # matches, \+ is treated like an ordinary +
185     "The interval is [0,1)." =~ /[0,1)./     # is a syntax error!
186     "The interval is [0,1)." =~ /\[0,1\)\./  # matches
187     "#!/usr/bin/perl" =~ /#!\/usr\/bin\/perl/;  # matches
188
189 In the last regexp, the forward slash C<'/'> is also backslashed,
190 because it is used to delimit the regexp.  This can lead to LTS
191 (leaning toothpick syndrome), however, and it is often more readable
192 to change delimiters.
193
194     "#!/usr/bin/perl" =~ m!#\!/usr/bin/perl!;  # easier to read
195
196 The backslash character C<'\'> is a metacharacter itself and needs to
197 be backslashed:
198
199     'C:\WIN32' =~ /C:\\WIN/;   # matches
200
201 In situations where it doesn't make sense for a particular metacharacter
202 to mean what it normally does, it automatically loses its
203 metacharacter-ness and becomes an ordinary character that is to be
204 matched literally.  For example, the C<'}'> is a metacharacter only when
205 it is the mate of a C<'{'> metacharacter.  Otherwise it is treated as a
206 literal RIGHT CURLY BRACKET.  This may lead to unexpected results.
207 L<C<use re 'strict'>|re/'strict' mode> can catch some of these.
208
209 In addition to the metacharacters, there are some ASCII characters
210 which don't have printable character equivalents and are instead
211 represented by I<escape sequences>.  Common examples are C<\t> for a
212 tab, C<\n> for a newline, C<\r> for a carriage return and C<\a> for a
213 bell (or alert).  If your string is better thought of as a sequence of arbitrary
214 bytes, the octal escape sequence, I<e.g.>, C<\033>, or hexadecimal escape
215 sequence, I<e.g.>, C<\x1B> may be a more natural representation for your
216 bytes.  Here are some examples of escapes:
217
218     "1000\t2000" =~ m(0\t2)   # matches
219     "1000\n2000" =~ /0\n20/   # matches
220     "1000\t2000" =~ /\000\t2/ # doesn't match, "0" ne "\000"
221     "cat"   =~ /\o{143}\x61\x74/ # matches in ASCII, but a weird way
222                                  # to spell cat
223
224 If you've been around Perl a while, all this talk of escape sequences
225 may seem familiar.  Similar escape sequences are used in double-quoted
226 strings and in fact the regexps in Perl are mostly treated as
227 double-quoted strings.  This means that variables can be used in
228 regexps as well.  Just like double-quoted strings, the values of the
229 variables in the regexp will be substituted in before the regexp is
230 evaluated for matching purposes.  So we have:
231
232     $foo = 'house';
233     'housecat' =~ /$foo/;      # matches
234     'cathouse' =~ /cat$foo/;   # matches
235     'housecat' =~ /${foo}cat/; # matches
236
237 So far, so good.  With the knowledge above you can already perform
238 searches with just about any literal string regexp you can dream up.
239 Here is a I<very simple> emulation of the Unix grep program:
240
241     % cat > simple_grep
242     #!/usr/bin/perl
243     $regexp = shift;
244     while (<>) {
245         print if /$regexp/;
246     }
247     ^D
248
249     % chmod +x simple_grep
250
251     % simple_grep abba /usr/dict/words
252     Babbage
253     cabbage
254     cabbages
255     sabbath
256     Sabbathize
257     Sabbathizes
258     sabbatical
259     scabbard
260     scabbards
261
262 This program is easy to understand.  C<#!/usr/bin/perl> is the standard
263 way to invoke a perl program from the shell.
264 S<C<$regexp = shift;>> saves the first command line argument as the
265 regexp to be used, leaving the rest of the command line arguments to
266 be treated as files.  S<C<< while (<>) >>> loops over all the lines in
267 all the files.  For each line, S<C<print if /$regexp/;>> prints the
268 line if the regexp matches the line.  In this line, both C<print> and
269 C</$regexp/> use the default variable C<$_> implicitly.
270
271 With all of the regexps above, if the regexp matched anywhere in the
272 string, it was considered a match.  Sometimes, however, we'd like to
273 specify I<where> in the string the regexp should try to match.  To do
274 this, we would use the I<anchor> metacharacters C<'^'> and C<'$'>.  The
275 anchor C<'^'> means match at the beginning of the string and the anchor
276 C<'$'> means match at the end of the string, or before a newline at the
277 end of the string.  Here is how they are used:
278
279     "housekeeper" =~ /keeper/;    # matches
280     "housekeeper" =~ /^keeper/;   # doesn't match
281     "housekeeper" =~ /keeper$/;   # matches
282     "housekeeper\n" =~ /keeper$/; # matches
283
284 The second regexp doesn't match because C<'^'> constrains C<keeper> to
285 match only at the beginning of the string, but C<"housekeeper"> has
286 keeper starting in the middle.  The third regexp does match, since the
287 C<'$'> constrains C<keeper> to match only at the end of the string.
288
289 When both C<'^'> and C<'$'> are used at the same time, the regexp has to
290 match both the beginning and the end of the string, I<i.e.>, the regexp
291 matches the whole string.  Consider
292
293     "keeper" =~ /^keep$/;      # doesn't match
294     "keeper" =~ /^keeper$/;    # matches
295     ""       =~ /^$/;          # ^$ matches an empty string
296
297 The first regexp doesn't match because the string has more to it than
298 C<keep>.  Since the second regexp is exactly the string, it
299 matches.  Using both C<'^'> and C<'$'> in a regexp forces the complete
300 string to match, so it gives you complete control over which strings
301 match and which don't.  Suppose you are looking for a fellow named
302 bert, off in a string by himself:
303
304     "dogbert" =~ /bert/;   # matches, but not what you want
305
306     "dilbert" =~ /^bert/;  # doesn't match, but ..
307     "bertram" =~ /^bert/;  # matches, so still not good enough
308
309     "bertram" =~ /^bert$/; # doesn't match, good
310     "dilbert" =~ /^bert$/; # doesn't match, good
311     "bert"    =~ /^bert$/; # matches, perfect
312
313 Of course, in the case of a literal string, one could just as easily
314 use the string comparison S<C<$string eq 'bert'>> and it would be
315 more efficient.   The  C<^...$> regexp really becomes useful when we
316 add in the more powerful regexp tools below.
317
318 =head2 Using character classes
319
320 Although one can already do quite a lot with the literal string
321 regexps above, we've only scratched the surface of regular expression
322 technology.  In this and subsequent sections we will introduce regexp
323 concepts (and associated metacharacter notations) that will allow a
324 regexp to represent not just a single character sequence, but a I<whole
325 class> of them.
326
327 One such concept is that of a I<character class>.  A character class
328 allows a set of possible characters, rather than just a single
329 character, to match at a particular point in a regexp.  You can define
330 your own custom character classes.  These
331 are denoted by brackets C<[...]>, with the set of characters
332 to be possibly matched inside.  Here are some examples:
333
334     /cat/;       # matches 'cat'
335     /[bcr]at/;   # matches 'bat, 'cat', or 'rat'
336     /item[0123456789]/;  # matches 'item0' or ... or 'item9'
337     "abc" =~ /[cab]/;    # matches 'a'
338
339 In the last statement, even though C<'c'> is the first character in
340 the class, C<'a'> matches because the first character position in the
341 string is the earliest point at which the regexp can match.
342
343     /[yY][eE][sS]/;      # match 'yes' in a case-insensitive way
344                          # 'yes', 'Yes', 'YES', etc.
345
346 This regexp displays a common task: perform a case-insensitive
347 match.  Perl provides a way of avoiding all those brackets by simply
348 appending an C<'i'> to the end of the match.  Then C</[yY][eE][sS]/;>
349 can be rewritten as C</yes/i;>.  The C<'i'> stands for
350 case-insensitive and is an example of a I<modifier> of the matching
351 operation.  We will meet other modifiers later in the tutorial.
352
353 We saw in the section above that there were ordinary characters, which
354 represented themselves, and special characters, which needed a
355 backslash C<'\'> to represent themselves.  The same is true in a
356 character class, but the sets of ordinary and special characters
357 inside a character class are different than those outside a character
358 class.  The special characters for a character class are C<-]\^$> (and
359 the pattern delimiter, whatever it is).
360 C<']'> is special because it denotes the end of a character class.  C<'$'> is
361 special because it denotes a scalar variable.  C<'\'> is special because
362 it is used in escape sequences, just like above.  Here is how the
363 special characters C<]$\> are handled:
364
365    /[\]c]def/; # matches ']def' or 'cdef'
366    $x = 'bcr';
367    /[$x]at/;   # matches 'bat', 'cat', or 'rat'
368    /[\$x]at/;  # matches '$at' or 'xat'
369    /[\\$x]at/; # matches '\at', 'bat, 'cat', or 'rat'
370
371 The last two are a little tricky.  In C<[\$x]>, the backslash protects
372 the dollar sign, so the character class has two members C<'$'> and C<'x'>.
373 In C<[\\$x]>, the backslash is protected, so C<$x> is treated as a
374 variable and substituted in double quote fashion.
375
376 The special character C<'-'> acts as a range operator within character
377 classes, so that a contiguous set of characters can be written as a
378 range.  With ranges, the unwieldy C<[0123456789]> and C<[abc...xyz]>
379 become the svelte C<[0-9]> and C<[a-z]>.  Some examples are
380
381     /item[0-9]/;  # matches 'item0' or ... or 'item9'
382     /[0-9bx-z]aa/;  # matches '0aa', ..., '9aa',
383                     # 'baa', 'xaa', 'yaa', or 'zaa'
384     /[0-9a-fA-F]/;  # matches a hexadecimal digit
385     /[0-9a-zA-Z_]/; # matches a "word" character,
386                     # like those in a Perl variable name
387
388 If C<'-'> is the first or last character in a character class, it is
389 treated as an ordinary character; C<[-ab]>, C<[ab-]> and C<[a\-b]> are
390 all equivalent.
391
392 The special character C<'^'> in the first position of a character class
393 denotes a I<negated character class>, which matches any character but
394 those in the brackets.  Both C<[...]> and C<[^...]> must match a
395 character, or the match fails.  Then
396
397     /[^a]at/;  # doesn't match 'aat' or 'at', but matches
398                # all other 'bat', 'cat, '0at', '%at', etc.
399     /[^0-9]/;  # matches a non-numeric character
400     /[a^]at/;  # matches 'aat' or '^at'; here '^' is ordinary
401
402 Now, even C<[0-9]> can be a bother to write multiple times, so in the
403 interest of saving keystrokes and making regexps more readable, Perl
404 has several abbreviations for common character classes, as shown below.
405 Since the introduction of Unicode, unless the C</a> modifier is in
406 effect, these character classes match more than just a few characters in
407 the ASCII range.
408
409 =over 4
410
411 =item *
412
413 C<\d> matches a digit, not just C<[0-9]> but also digits from non-roman scripts
414
415 =item *
416
417 C<\s> matches a whitespace character, the set C<[\ \t\r\n\f]> and others
418
419 =item *
420
421 C<\w> matches a word character (alphanumeric or C<'_'>), not just C<[0-9a-zA-Z_]>
422 but also digits and characters from non-roman scripts
423
424 =item *
425
426 C<\D> is a negated C<\d>; it represents any other character than a digit, or C<[^\d]>
427
428 =item *
429
430 C<\S> is a negated C<\s>; it represents any non-whitespace character C<[^\s]>
431
432 =item *
433
434 C<\W> is a negated C<\w>; it represents any non-word character C<[^\w]>
435
436 =item *
437
438 The period C<'.'> matches any character but C<"\n"> (unless the modifier C</s> is
439 in effect, as explained below).
440
441 =item *
442
443 C<\N>, like the period, matches any character but C<"\n">, but it does so
444 regardless of whether the modifier C</s> is in effect.
445
446 =back
447
448 The C</a> modifier, available starting in Perl 5.14,  is used to
449 restrict the matches of C<\d>, C<\s>, and C<\w> to just those in the ASCII range.
450 It is useful to keep your program from being needlessly exposed to full
451 Unicode (and its accompanying security considerations) when all you want
452 is to process English-like text.  (The "a" may be doubled, C</aa>, to
453 provide even more restrictions, preventing case-insensitive matching of
454 ASCII with non-ASCII characters; otherwise a Unicode "Kelvin Sign"
455 would caselessly match a "k" or "K".)
456
457 The C<\d\s\w\D\S\W> abbreviations can be used both inside and outside
458 of bracketed character classes.  Here are some in use:
459
460     /\d\d:\d\d:\d\d/; # matches a hh:mm:ss time format
461     /[\d\s]/;         # matches any digit or whitespace character
462     /\w\W\w/;         # matches a word char, followed by a
463                       # non-word char, followed by a word char
464     /..rt/;           # matches any two chars, followed by 'rt'
465     /end\./;          # matches 'end.'
466     /end[.]/;         # same thing, matches 'end.'
467
468 Because a period is a metacharacter, it needs to be escaped to match
469 as an ordinary period. Because, for example, C<\d> and C<\w> are sets
470 of characters, it is incorrect to think of C<[^\d\w]> as C<[\D\W]>; in
471 fact C<[^\d\w]> is the same as C<[^\w]>, which is the same as
472 C<[\W]>. Think DeMorgan's laws.
473
474 In actuality, the period and C<\d\s\w\D\S\W> abbreviations are
475 themselves types of character classes, so the ones surrounded by
476 brackets are just one type of character class.  When we need to make a
477 distinction, we refer to them as "bracketed character classes."
478
479 An anchor useful in basic regexps is the I<word anchor>
480 C<\b>.  This matches a boundary between a word character and a non-word
481 character C<\w\W> or C<\W\w>:
482
483     $x = "Housecat catenates house and cat";
484     $x =~ /cat/;    # matches cat in 'housecat'
485     $x =~ /\bcat/;  # matches cat in 'catenates'
486     $x =~ /cat\b/;  # matches cat in 'housecat'
487     $x =~ /\bcat\b/;  # matches 'cat' at end of string
488
489 Note in the last example, the end of the string is considered a word
490 boundary.
491
492 For natural language processing (so that, for example, apostrophes are
493 included in words), use instead C<\b{wb}>
494
495     "don't" =~ / .+? \b{wb} /x;  # matches the whole string
496
497 You might wonder why C<'.'> matches everything but C<"\n"> - why not
498 every character? The reason is that often one is matching against
499 lines and would like to ignore the newline characters.  For instance,
500 while the string C<"\n"> represents one line, we would like to think
501 of it as empty.  Then
502
503     ""   =~ /^$/;    # matches
504     "\n" =~ /^$/;    # matches, $ anchors before "\n"
505
506     ""   =~ /./;      # doesn't match; it needs a char
507     ""   =~ /^.$/;    # doesn't match; it needs a char
508     "\n" =~ /^.$/;    # doesn't match; it needs a char other than "\n"
509     "a"  =~ /^.$/;    # matches
510     "a\n"  =~ /^.$/;  # matches, $ anchors before "\n"
511
512 This behavior is convenient, because we usually want to ignore
513 newlines when we count and match characters in a line.  Sometimes,
514 however, we want to keep track of newlines.  We might even want C<'^'>
515 and C<'$'> to anchor at the beginning and end of lines within the
516 string, rather than just the beginning and end of the string.  Perl
517 allows us to choose between ignoring and paying attention to newlines
518 by using the C</s> and C</m> modifiers.  C</s> and C</m> stand for
519 single line and multi-line and they determine whether a string is to
520 be treated as one continuous string, or as a set of lines.  The two
521 modifiers affect two aspects of how the regexp is interpreted: 1) how
522 the C<'.'> character class is defined, and 2) where the anchors C<'^'>
523 and C<'$'> are able to match.  Here are the four possible combinations:
524
525 =over 4
526
527 =item *
528
529 no modifiers: Default behavior.  C<'.'> matches any character
530 except C<"\n">.  C<'^'> matches only at the beginning of the string and
531 C<'$'> matches only at the end or before a newline at the end.
532
533 =item *
534
535 s modifier (C</s>): Treat string as a single long line.  C<'.'> matches
536 any character, even C<"\n">.  C<'^'> matches only at the beginning of
537 the string and C<'$'> matches only at the end or before a newline at the
538 end.
539
540 =item *
541
542 m modifier (C</m>): Treat string as a set of multiple lines.  C<'.'>
543 matches any character except C<"\n">.  C<'^'> and C<'$'> are able to match
544 at the start or end of I<any> line within the string.
545
546 =item *
547
548 both s and m modifiers (C</sm>): Treat string as a single long line, but
549 detect multiple lines.  C<'.'> matches any character, even
550 C<"\n">.  C<'^'> and C<'$'>, however, are able to match at the start or end
551 of I<any> line within the string.
552
553 =back
554
555 Here are examples of C</s> and C</m> in action:
556
557     $x = "There once was a girl\nWho programmed in Perl\n";
558
559     $x =~ /^Who/;   # doesn't match, "Who" not at start of string
560     $x =~ /^Who/s;  # doesn't match, "Who" not at start of string
561     $x =~ /^Who/m;  # matches, "Who" at start of second line
562     $x =~ /^Who/sm; # matches, "Who" at start of second line
563
564     $x =~ /girl.Who/;   # doesn't match, "." doesn't match "\n"
565     $x =~ /girl.Who/s;  # matches, "." matches "\n"
566     $x =~ /girl.Who/m;  # doesn't match, "." doesn't match "\n"
567     $x =~ /girl.Who/sm; # matches, "." matches "\n"
568
569 Most of the time, the default behavior is what is wanted, but C</s> and
570 C</m> are occasionally very useful.  If C</m> is being used, the start
571 of the string can still be matched with C<\A> and the end of the string
572 can still be matched with the anchors C<\Z> (matches both the end and
573 the newline before, like C<'$'>), and C<\z> (matches only the end):
574
575     $x =~ /^Who/m;   # matches, "Who" at start of second line
576     $x =~ /\AWho/m;  # doesn't match, "Who" is not at start of string
577
578     $x =~ /girl$/m;  # matches, "girl" at end of first line
579     $x =~ /girl\Z/m; # doesn't match, "girl" is not at end of string
580
581     $x =~ /Perl\Z/m; # matches, "Perl" is at newline before end
582     $x =~ /Perl\z/m; # doesn't match, "Perl" is not at end of string
583
584 We now know how to create choices among classes of characters in a
585 regexp.  What about choices among words or character strings? Such
586 choices are described in the next section.
587
588 =head2 Matching this or that
589
590 Sometimes we would like our regexp to be able to match different
591 possible words or character strings.  This is accomplished by using
592 the I<alternation> metacharacter C<'|'>.  To match C<dog> or C<cat>, we
593 form the regexp C<dog|cat>.  As before, Perl will try to match the
594 regexp at the earliest possible point in the string.  At each
595 character position, Perl will first try to match the first
596 alternative, C<dog>.  If C<dog> doesn't match, Perl will then try the
597 next alternative, C<cat>.  If C<cat> doesn't match either, then the
598 match fails and Perl moves to the next position in the string.  Some
599 examples:
600
601     "cats and dogs" =~ /cat|dog|bird/;  # matches "cat"
602     "cats and dogs" =~ /dog|cat|bird/;  # matches "cat"
603
604 Even though C<dog> is the first alternative in the second regexp,
605 C<cat> is able to match earlier in the string.
606
607     "cats"          =~ /c|ca|cat|cats/; # matches "c"
608     "cats"          =~ /cats|cat|ca|c/; # matches "cats"
609
610 Here, all the alternatives match at the first string position, so the
611 first alternative is the one that matches.  If some of the
612 alternatives are truncations of the others, put the longest ones first
613 to give them a chance to match.
614
615     "cab" =~ /a|b|c/ # matches "c"
616                      # /a|b|c/ == /[abc]/
617
618 The last example points out that character classes are like
619 alternations of characters.  At a given character position, the first
620 alternative that allows the regexp match to succeed will be the one
621 that matches.
622
623 =head2 Grouping things and hierarchical matching
624
625 Alternation allows a regexp to choose among alternatives, but by
626 itself it is unsatisfying.  The reason is that each alternative is a whole
627 regexp, but sometime we want alternatives for just part of a
628 regexp.  For instance, suppose we want to search for housecats or
629 housekeepers.  The regexp C<housecat|housekeeper> fits the bill, but is
630 inefficient because we had to type C<house> twice.  It would be nice to
631 have parts of the regexp be constant, like C<house>, and some
632 parts have alternatives, like C<cat|keeper>.
633
634 The I<grouping> metacharacters C<()> solve this problem.  Grouping
635 allows parts of a regexp to be treated as a single unit.  Parts of a
636 regexp are grouped by enclosing them in parentheses.  Thus we could solve
637 the C<housecat|housekeeper> by forming the regexp as
638 C<house(cat|keeper)>.  The regexp C<house(cat|keeper)> means match
639 C<house> followed by either C<cat> or C<keeper>.  Some more examples
640 are
641
642     /(a|b)b/;    # matches 'ab' or 'bb'
643     /(ac|b)b/;   # matches 'acb' or 'bb'
644     /(^a|b)c/;   # matches 'ac' at start of string or 'bc' anywhere
645     /(a|[bc])d/; # matches 'ad', 'bd', or 'cd'
646
647     /house(cat|)/;  # matches either 'housecat' or 'house'
648     /house(cat(s|)|)/;  # matches either 'housecats' or 'housecat' or
649                         # 'house'.  Note groups can be nested.
650
651     /(19|20|)\d\d/;  # match years 19xx, 20xx, or the Y2K problem, xx
652     "20" =~ /(19|20|)\d\d/;  # matches the null alternative '()\d\d',
653                              # because '20\d\d' can't match
654
655 Alternations behave the same way in groups as out of them: at a given
656 string position, the leftmost alternative that allows the regexp to
657 match is taken.  So in the last example at the first string position,
658 C<"20"> matches the second alternative, but there is nothing left over
659 to match the next two digits C<\d\d>.  So Perl moves on to the next
660 alternative, which is the null alternative and that works, since
661 C<"20"> is two digits.
662
663 The process of trying one alternative, seeing if it matches, and
664 moving on to the next alternative, while going back in the string
665 from where the previous alternative was tried, if it doesn't, is called
666 I<backtracking>.  The term "backtracking" comes from the idea that
667 matching a regexp is like a walk in the woods.  Successfully matching
668 a regexp is like arriving at a destination.  There are many possible
669 trailheads, one for each string position, and each one is tried in
670 order, left to right.  From each trailhead there may be many paths,
671 some of which get you there, and some which are dead ends.  When you
672 walk along a trail and hit a dead end, you have to backtrack along the
673 trail to an earlier point to try another trail.  If you hit your
674 destination, you stop immediately and forget about trying all the
675 other trails.  You are persistent, and only if you have tried all the
676 trails from all the trailheads and not arrived at your destination, do
677 you declare failure.  To be concrete, here is a step-by-step analysis
678 of what Perl does when it tries to match the regexp
679
680     "abcde" =~ /(abd|abc)(df|d|de)/;
681
682 =over 4
683
684 =item Z<>0. Start with the first letter in the string C<'a'>.
685
686 E<nbsp>
687
688 =item Z<>1. Try the first alternative in the first group C<'abd'>.
689
690 E<nbsp>
691
692 =item Z<>2.  Match C<'a'> followed by C<'b'>. So far so good.
693
694 E<nbsp>
695
696 =item Z<>3.  C<'d'> in the regexp doesn't match C<'c'> in the string - a
697 dead end.  So backtrack two characters and pick the second alternative
698 in the first group C<'abc'>.
699
700 E<nbsp>
701
702 =item Z<>4.  Match C<'a'> followed by C<'b'> followed by C<'c'>.  We are on a roll
703 and have satisfied the first group. Set C<$1> to C<'abc'>.
704
705 E<nbsp>
706
707 =item Z<>5 Move on to the second group and pick the first alternative C<'df'>.
708
709 E<nbsp>
710
711 =item Z<>6 Match the C<'d'>.
712
713 E<nbsp>
714
715 =item Z<>7.  C<'f'> in the regexp doesn't match C<'e'> in the string, so a dead
716 end.  Backtrack one character and pick the second alternative in the
717 second group C<'d'>.
718
719 E<nbsp>
720
721 =item Z<>8.  C<'d'> matches. The second grouping is satisfied, so set
722 C<$2> to C<'d'>.
723
724 E<nbsp>
725
726 =item Z<>9.  We are at the end of the regexp, so we are done! We have
727 matched C<'abcd'> out of the string C<"abcde">.
728
729 =back
730
731 There are a couple of things to note about this analysis.  First, the
732 third alternative in the second group C<'de'> also allows a match, but we
733 stopped before we got to it - at a given character position, leftmost
734 wins.  Second, we were able to get a match at the first character
735 position of the string C<'a'>.  If there were no matches at the first
736 position, Perl would move to the second character position C<'b'> and
737 attempt the match all over again.  Only when all possible paths at all
738 possible character positions have been exhausted does Perl give
739 up and declare S<C<$string =~ /(abd|abc)(df|d|de)/;>> to be false.
740
741 Even with all this work, regexp matching happens remarkably fast.  To
742 speed things up, Perl compiles the regexp into a compact sequence of
743 opcodes that can often fit inside a processor cache.  When the code is
744 executed, these opcodes can then run at full throttle and search very
745 quickly.
746
747 =head2 Extracting matches
748
749 The grouping metacharacters C<()> also serve another completely
750 different function: they allow the extraction of the parts of a string
751 that matched.  This is very useful to find out what matched and for
752 text processing in general.  For each grouping, the part that matched
753 inside goes into the special variables C<$1>, C<$2>, I<etc>.  They can be
754 used just as ordinary variables:
755
756     # extract hours, minutes, seconds
757     if ($time =~ /(\d\d):(\d\d):(\d\d)/) {    # match hh:mm:ss format
758         $hours = $1;
759         $minutes = $2;
760         $seconds = $3;
761     }
762
763 Now, we know that in scalar context,
764 S<C<$time =~ /(\d\d):(\d\d):(\d\d)/>> returns a true or false
765 value.  In list context, however, it returns the list of matched values
766 C<($1,$2,$3)>.  So we could write the code more compactly as
767
768     # extract hours, minutes, seconds
769     ($hours, $minutes, $second) = ($time =~ /(\d\d):(\d\d):(\d\d)/);
770
771 If the groupings in a regexp are nested, C<$1> gets the group with the
772 leftmost opening parenthesis, C<$2> the next opening parenthesis,
773 I<etc>.  Here is a regexp with nested groups:
774
775     /(ab(cd|ef)((gi)|j))/;
776      1  2      34
777
778 If this regexp matches, C<$1> contains a string starting with
779 C<'ab'>, C<$2> is either set to C<'cd'> or C<'ef'>, C<$3> equals either
780 C<'gi'> or C<'j'>, and C<$4> is either set to C<'gi'>, just like C<$3>,
781 or it remains undefined.
782
783 For convenience, Perl sets C<$+> to the string held by the highest numbered
784 C<$1>, C<$2>,... that got assigned (and, somewhat related, C<$^N> to the
785 value of the C<$1>, C<$2>,... most-recently assigned; I<i.e.> the C<$1>,
786 C<$2>,... associated with the rightmost closing parenthesis used in the
787 match).
788
789
790 =head2 Backreferences
791
792 Closely associated with the matching variables C<$1>, C<$2>, ... are
793 the I<backreferences> C<\g1>, C<\g2>,...  Backreferences are simply
794 matching variables that can be used I<inside> a regexp.  This is a
795 really nice feature; what matches later in a regexp is made to depend on
796 what matched earlier in the regexp.  Suppose we wanted to look
797 for doubled words in a text, like "the the".  The following regexp finds
798 all 3-letter doubles with a space in between:
799
800     /\b(\w\w\w)\s\g1\b/;
801
802 The grouping assigns a value to C<\g1>, so that the same 3-letter sequence
803 is used for both parts.
804
805 A similar task is to find words consisting of two identical parts:
806
807     % simple_grep '^(\w\w\w\w|\w\w\w|\w\w|\w)\g1$' /usr/dict/words
808     beriberi
809     booboo
810     coco
811     mama
812     murmur
813     papa
814
815 The regexp has a single grouping which considers 4-letter
816 combinations, then 3-letter combinations, I<etc>., and uses C<\g1> to look for
817 a repeat.  Although C<$1> and C<\g1> represent the same thing, care should be
818 taken to use matched variables C<$1>, C<$2>,... only I<outside> a regexp
819 and backreferences C<\g1>, C<\g2>,... only I<inside> a regexp; not doing
820 so may lead to surprising and unsatisfactory results.
821
822
823 =head2 Relative backreferences
824
825 Counting the opening parentheses to get the correct number for a
826 backreference is error-prone as soon as there is more than one
827 capturing group.  A more convenient technique became available
828 with Perl 5.10: relative backreferences. To refer to the immediately
829 preceding capture group one now may write C<\g{-1}>, the next but
830 last is available via C<\g{-2}>, and so on.
831
832 Another good reason in addition to readability and maintainability
833 for using relative backreferences is illustrated by the following example,
834 where a simple pattern for matching peculiar strings is used:
835
836     $a99a = '([a-z])(\d)\g2\g1';   # matches a11a, g22g, x33x, etc.
837
838 Now that we have this pattern stored as a handy string, we might feel
839 tempted to use it as a part of some other pattern:
840
841     $line = "code=e99e";
842     if ($line =~ /^(\w+)=$a99a$/){   # unexpected behavior!
843         print "$1 is valid\n";
844     } else {
845         print "bad line: '$line'\n";
846     }
847
848 But this doesn't match, at least not the way one might expect. Only
849 after inserting the interpolated C<$a99a> and looking at the resulting
850 full text of the regexp is it obvious that the backreferences have
851 backfired. The subexpression C<(\w+)> has snatched number 1 and
852 demoted the groups in C<$a99a> by one rank. This can be avoided by
853 using relative backreferences:
854
855     $a99a = '([a-z])(\d)\g{-1}\g{-2}';  # safe for being interpolated
856
857
858 =head2 Named backreferences
859
860 Perl 5.10 also introduced named capture groups and named backreferences.
861 To attach a name to a capturing group, you write either
862 C<< (?<name>...) >> or C<< (?'name'...) >>.  The backreference may
863 then be written as C<\g{name}>.  It is permissible to attach the
864 same name to more than one group, but then only the leftmost one of the
865 eponymous set can be referenced.  Outside of the pattern a named
866 capture group is accessible through the C<%+> hash.
867
868 Assuming that we have to match calendar dates which may be given in one
869 of the three formats yyyy-mm-dd, mm/dd/yyyy or dd.mm.yyyy, we can write
870 three suitable patterns where we use C<'d'>, C<'m'> and C<'y'> respectively as the
871 names of the groups capturing the pertaining components of a date. The
872 matching operation combines the three patterns as alternatives:
873
874     $fmt1 = '(?<y>\d\d\d\d)-(?<m>\d\d)-(?<d>\d\d)';
875     $fmt2 = '(?<m>\d\d)/(?<d>\d\d)/(?<y>\d\d\d\d)';
876     $fmt3 = '(?<d>\d\d)\.(?<m>\d\d)\.(?<y>\d\d\d\d)';
877     for my $d qw( 2006-10-21 15.01.2007 10/31/2005 ){
878         if ( $d =~ m{$fmt1|$fmt2|$fmt3} ){
879             print "day=$+{d} month=$+{m} year=$+{y}\n";
880         }
881     }
882
883 If any of the alternatives matches, the hash C<%+> is bound to contain the
884 three key-value pairs.
885
886
887 =head2 Alternative capture group numbering
888
889 Yet another capturing group numbering technique (also as from Perl 5.10)
890 deals with the problem of referring to groups within a set of alternatives.
891 Consider a pattern for matching a time of the day, civil or military style:
892
893     if ( $time =~ /(\d\d|\d):(\d\d)|(\d\d)(\d\d)/ ){
894         # process hour and minute
895     }
896
897 Processing the results requires an additional if statement to determine
898 whether C<$1> and C<$2> or C<$3> and C<$4> contain the goodies. It would
899 be easier if we could use group numbers 1 and 2 in second alternative as
900 well, and this is exactly what the parenthesized construct C<(?|...)>,
901 set around an alternative achieves. Here is an extended version of the
902 previous pattern:
903
904   if($time =~ /(?|(\d\d|\d):(\d\d)|(\d\d)(\d\d))\s+([A-Z][A-Z][A-Z])/){
905       print "hour=$1 minute=$2 zone=$3\n";
906   }
907
908 Within the alternative numbering group, group numbers start at the same
909 position for each alternative. After the group, numbering continues
910 with one higher than the maximum reached across all the alternatives.
911
912 =head2 Position information
913
914 In addition to what was matched, Perl also provides the
915 positions of what was matched as contents of the C<@-> and C<@+>
916 arrays. C<$-[0]> is the position of the start of the entire match and
917 C<$+[0]> is the position of the end. Similarly, C<$-[n]> is the
918 position of the start of the C<$n> match and C<$+[n]> is the position
919 of the end. If C<$n> is undefined, so are C<$-[n]> and C<$+[n]>. Then
920 this code
921
922     $x = "Mmm...donut, thought Homer";
923     $x =~ /^(Mmm|Yech)\.\.\.(donut|peas)/; # matches
924     foreach $exp (1..$#-) {
925         print "Match $exp: '${$exp}' at position ($-[$exp],$+[$exp])\n";
926     }
927
928 prints
929
930     Match 1: 'Mmm' at position (0,3)
931     Match 2: 'donut' at position (6,11)
932
933 Even if there are no groupings in a regexp, it is still possible to
934 find out what exactly matched in a string.  If you use them, Perl
935 will set C<$`> to the part of the string before the match, will set C<$&>
936 to the part of the string that matched, and will set C<'$'> to the part
937 of the string after the match.  An example:
938
939     $x = "the cat caught the mouse";
940     $x =~ /cat/;  # $` = 'the ', $& = 'cat', $' = ' caught the mouse'
941     $x =~ /the/;  # $` = '', $& = 'the', $' = ' cat caught the mouse'
942
943 In the second match, C<$`> equals C<''> because the regexp matched at the
944 first character position in the string and stopped; it never saw the
945 second "the".
946
947 If your code is to run on Perl versions earlier than
948 5.20, it is worthwhile to note that using C<$`> and C<'$'>
949 slows down regexp matching quite a bit, while C<$&> slows it down to a
950 lesser extent, because if they are used in one regexp in a program,
951 they are generated for I<all> regexps in the program.  So if raw
952 performance is a goal of your application, they should be avoided.
953 If you need to extract the corresponding substrings, use C<@-> and
954 C<@+> instead:
955
956     $` is the same as substr( $x, 0, $-[0] )
957     $& is the same as substr( $x, $-[0], $+[0]-$-[0] )
958     $' is the same as substr( $x, $+[0] )
959
960 As of Perl 5.10, the C<${^PREMATCH}>, C<${^MATCH}> and C<${^POSTMATCH}>
961 variables may be used.  These are only set if the C</p> modifier is
962 present.  Consequently they do not penalize the rest of the program.  In
963 Perl 5.20, C<${^PREMATCH}>, C<${^MATCH}> and C<${^POSTMATCH}> are available
964 whether the C</p> has been used or not (the modifier is ignored), and
965 C<$`>, C<'$'> and C<$&> do not cause any speed difference.
966
967 =head2 Non-capturing groupings
968
969 A group that is required to bundle a set of alternatives may or may not be
970 useful as a capturing group.  If it isn't, it just creates a superfluous
971 addition to the set of available capture group values, inside as well as
972 outside the regexp.  Non-capturing groupings, denoted by C<(?:regexp)>,
973 still allow the regexp to be treated as a single unit, but don't establish
974 a capturing group at the same time.  Both capturing and non-capturing
975 groupings are allowed to co-exist in the same regexp.  Because there is
976 no extraction, non-capturing groupings are faster than capturing
977 groupings.  Non-capturing groupings are also handy for choosing exactly
978 which parts of a regexp are to be extracted to matching variables:
979
980     # match a number, $1-$4 are set, but we only want $1
981     /([+-]?\ *(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?)/;
982
983     # match a number faster , only $1 is set
984     /([+-]?\ *(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?)/;
985
986     # match a number, get $1 = whole number, $2 = exponent
987     /([+-]?\ *(?:\d+(?:\.\d*)?|\.\d+)(?:[eE]([+-]?\d+))?)/;
988
989 Non-capturing groupings are also useful for removing nuisance
990 elements gathered from a split operation where parentheses are
991 required for some reason:
992
993     $x = '12aba34ba5';
994     @num = split /(a|b)+/, $x;    # @num = ('12','a','34','a','5')
995     @num = split /(?:a|b)+/, $x;  # @num = ('12','34','5')
996
997 In Perl 5.22 and later, all groups within a regexp can be set to
998 non-capturing by using the new C</n> flag:
999
1000     "hello" =~ /(hi|hello)/n; # $1 is not set!
1001
1002 See L<perlre/"n"> for more information.
1003
1004 =head2 Matching repetitions
1005
1006 The examples in the previous section display an annoying weakness.  We
1007 were only matching 3-letter words, or chunks of words of 4 letters or
1008 less.  We'd like to be able to match words or, more generally, strings
1009 of any length, without writing out tedious alternatives like
1010 C<\w\w\w\w|\w\w\w|\w\w|\w>.
1011
1012 This is exactly the problem the I<quantifier> metacharacters C<'?'>,
1013 C<'*'>, C<'+'>, and C<{}> were created for.  They allow us to delimit the
1014 number of repeats for a portion of a regexp we consider to be a
1015 match.  Quantifiers are put immediately after the character, character
1016 class, or grouping that we want to specify.  They have the following
1017 meanings:
1018
1019 =over 4
1020
1021 =item *
1022
1023 C<a?> means: match C<'a'> 1 or 0 times
1024
1025 =item *
1026
1027 C<a*> means: match C<'a'> 0 or more times, I<i.e.>, any number of times
1028
1029 =item *
1030
1031 C<a+> means: match C<'a'> 1 or more times, I<i.e.>, at least once
1032
1033 =item *
1034
1035 C<a{n,m}> means: match at least C<n> times, but not more than C<m>
1036 times.
1037
1038 =item *
1039
1040 C<a{n,}> means: match at least C<n> or more times
1041
1042 =item *
1043
1044 C<a{n}> means: match exactly C<n> times
1045
1046 =back
1047
1048 Here are some examples:
1049
1050     /[a-z]+\s+\d*/;  # match a lowercase word, at least one space, and
1051                      # any number of digits
1052     /(\w+)\s+\g1/;    # match doubled words of arbitrary length
1053     /y(es)?/i;       # matches 'y', 'Y', or a case-insensitive 'yes'
1054     $year =~ /^\d{2,4}$/;  # make sure year is at least 2 but not more
1055                            # than 4 digits
1056     $year =~ /^\d{4}$|^\d{2}$/; # better match; throw out 3-digit dates
1057     $year =~ /^\d{2}(\d{2})?$/; # same thing written differently.
1058                                 # However, this captures the last two
1059                                 # digits in $1 and the other does not.
1060
1061     % simple_grep '^(\w+)\g1$' /usr/dict/words   # isn't this easier?
1062     beriberi
1063     booboo
1064     coco
1065     mama
1066     murmur
1067     papa
1068
1069 For all of these quantifiers, Perl will try to match as much of the
1070 string as possible, while still allowing the regexp to succeed.  Thus
1071 with C</a?.../>, Perl will first try to match the regexp with the C<'a'>
1072 present; if that fails, Perl will try to match the regexp without the
1073 C<'a'> present.  For the quantifier C<'*'>, we get the following:
1074
1075     $x = "the cat in the hat";
1076     $x =~ /^(.*)(cat)(.*)$/; # matches,
1077                              # $1 = 'the '
1078                              # $2 = 'cat'
1079                              # $3 = ' in the hat'
1080
1081 Which is what we might expect, the match finds the only C<cat> in the
1082 string and locks onto it.  Consider, however, this regexp:
1083
1084     $x =~ /^(.*)(at)(.*)$/; # matches,
1085                             # $1 = 'the cat in the h'
1086                             # $2 = 'at'
1087                             # $3 = ''   (0 characters match)
1088
1089 One might initially guess that Perl would find the C<at> in C<cat> and
1090 stop there, but that wouldn't give the longest possible string to the
1091 first quantifier C<.*>.  Instead, the first quantifier C<.*> grabs as
1092 much of the string as possible while still having the regexp match.  In
1093 this example, that means having the C<at> sequence with the final C<at>
1094 in the string.  The other important principle illustrated here is that,
1095 when there are two or more elements in a regexp, the I<leftmost>
1096 quantifier, if there is one, gets to grab as much of the string as
1097 possible, leaving the rest of the regexp to fight over scraps.  Thus in
1098 our example, the first quantifier C<.*> grabs most of the string, while
1099 the second quantifier C<.*> gets the empty string.   Quantifiers that
1100 grab as much of the string as possible are called I<maximal match> or
1101 I<greedy> quantifiers.
1102
1103 When a regexp can match a string in several different ways, we can use
1104 the principles above to predict which way the regexp will match:
1105
1106 =over 4
1107
1108 =item *
1109
1110 Principle 0: Taken as a whole, any regexp will be matched at the
1111 earliest possible position in the string.
1112
1113 =item *
1114
1115 Principle 1: In an alternation C<a|b|c...>, the leftmost alternative
1116 that allows a match for the whole regexp will be the one used.
1117
1118 =item *
1119
1120 Principle 2: The maximal matching quantifiers C<'?'>, C<'*'>, C<'+'> and
1121 C<{n,m}> will in general match as much of the string as possible while
1122 still allowing the whole regexp to match.
1123
1124 =item *
1125
1126 Principle 3: If there are two or more elements in a regexp, the
1127 leftmost greedy quantifier, if any, will match as much of the string
1128 as possible while still allowing the whole regexp to match.  The next
1129 leftmost greedy quantifier, if any, will try to match as much of the
1130 string remaining available to it as possible, while still allowing the
1131 whole regexp to match.  And so on, until all the regexp elements are
1132 satisfied.
1133
1134 =back
1135
1136 As we have seen above, Principle 0 overrides the others. The regexp
1137 will be matched as early as possible, with the other principles
1138 determining how the regexp matches at that earliest character
1139 position.
1140
1141 Here is an example of these principles in action:
1142
1143     $x = "The programming republic of Perl";
1144     $x =~ /^(.+)(e|r)(.*)$/;  # matches,
1145                               # $1 = 'The programming republic of Pe'
1146                               # $2 = 'r'
1147                               # $3 = 'l'
1148
1149 This regexp matches at the earliest string position, C<'T'>.  One
1150 might think that C<'e'>, being leftmost in the alternation, would be
1151 matched, but C<'r'> produces the longest string in the first quantifier.
1152
1153     $x =~ /(m{1,2})(.*)$/;  # matches,
1154                             # $1 = 'mm'
1155                             # $2 = 'ing republic of Perl'
1156
1157 Here, The earliest possible match is at the first C<'m'> in
1158 C<programming>. C<m{1,2}> is the first quantifier, so it gets to match
1159 a maximal C<mm>.
1160
1161     $x =~ /.*(m{1,2})(.*)$/;  # matches,
1162                               # $1 = 'm'
1163                               # $2 = 'ing republic of Perl'
1164
1165 Here, the regexp matches at the start of the string. The first
1166 quantifier C<.*> grabs as much as possible, leaving just a single
1167 C<'m'> for the second quantifier C<m{1,2}>.
1168
1169     $x =~ /(.?)(m{1,2})(.*)$/;  # matches,
1170                                 # $1 = 'a'
1171                                 # $2 = 'mm'
1172                                 # $3 = 'ing republic of Perl'
1173
1174 Here, C<.?> eats its maximal one character at the earliest possible
1175 position in the string, C<'a'> in C<programming>, leaving C<m{1,2}>
1176 the opportunity to match both C<'m'>'s. Finally,
1177
1178     "aXXXb" =~ /(X*)/; # matches with $1 = ''
1179
1180 because it can match zero copies of C<'X'> at the beginning of the
1181 string.  If you definitely want to match at least one C<'X'>, use
1182 C<X+>, not C<X*>.
1183
1184 Sometimes greed is not good.  At times, we would like quantifiers to
1185 match a I<minimal> piece of string, rather than a maximal piece.  For
1186 this purpose, Larry Wall created the I<minimal match> or
1187 I<non-greedy> quantifiers C<??>, C<*?>, C<+?>, and C<{}?>.  These are
1188 the usual quantifiers with a C<'?'> appended to them.  They have the
1189 following meanings:
1190
1191 =over 4
1192
1193 =item *
1194
1195 C<a??> means: match C<'a'> 0 or 1 times. Try 0 first, then 1.
1196
1197 =item *
1198
1199 C<a*?> means: match C<'a'> 0 or more times, I<i.e.>, any number of times,
1200 but as few times as possible
1201
1202 =item *
1203
1204 C<a+?> means: match C<'a'> 1 or more times, I<i.e.>, at least once, but
1205 as few times as possible
1206
1207 =item *
1208
1209 C<a{n,m}?> means: match at least C<n> times, not more than C<m>
1210 times, as few times as possible
1211
1212 =item *
1213
1214 C<a{n,}?> means: match at least C<n> times, but as few times as
1215 possible
1216
1217 =item *
1218
1219 C<a{n}?> means: match exactly C<n> times.  Because we match exactly
1220 C<n> times, C<a{n}?> is equivalent to C<a{n}> and is just there for
1221 notational consistency.
1222
1223 =back
1224
1225 Let's look at the example above, but with minimal quantifiers:
1226
1227     $x = "The programming republic of Perl";
1228     $x =~ /^(.+?)(e|r)(.*)$/; # matches,
1229                               # $1 = 'Th'
1230                               # $2 = 'e'
1231                               # $3 = ' programming republic of Perl'
1232
1233 The minimal string that will allow both the start of the string C<'^'>
1234 and the alternation to match is C<Th>, with the alternation C<e|r>
1235 matching C<'e'>.  The second quantifier C<.*> is free to gobble up the
1236 rest of the string.
1237
1238     $x =~ /(m{1,2}?)(.*?)$/;  # matches,
1239                               # $1 = 'm'
1240                               # $2 = 'ming republic of Perl'
1241
1242 The first string position that this regexp can match is at the first
1243 C<'m'> in C<programming>. At this position, the minimal C<m{1,2}?>
1244 matches just one C<'m'>.  Although the second quantifier C<.*?> would
1245 prefer to match no characters, it is constrained by the end-of-string
1246 anchor C<'$'> to match the rest of the string.
1247
1248     $x =~ /(.*?)(m{1,2}?)(.*)$/;  # matches,
1249                                   # $1 = 'The progra'
1250                                   # $2 = 'm'
1251                                   # $3 = 'ming republic of Perl'
1252
1253 In this regexp, you might expect the first minimal quantifier C<.*?>
1254 to match the empty string, because it is not constrained by a C<'^'>
1255 anchor to match the beginning of the word.  Principle 0 applies here,
1256 however.  Because it is possible for the whole regexp to match at the
1257 start of the string, it I<will> match at the start of the string.  Thus
1258 the first quantifier has to match everything up to the first C<'m'>.  The
1259 second minimal quantifier matches just one C<'m'> and the third
1260 quantifier matches the rest of the string.
1261
1262     $x =~ /(.??)(m{1,2})(.*)$/;  # matches,
1263                                  # $1 = 'a'
1264                                  # $2 = 'mm'
1265                                  # $3 = 'ing republic of Perl'
1266
1267 Just as in the previous regexp, the first quantifier C<.??> can match
1268 earliest at position C<'a'>, so it does.  The second quantifier is
1269 greedy, so it matches C<mm>, and the third matches the rest of the
1270 string.
1271
1272 We can modify principle 3 above to take into account non-greedy
1273 quantifiers:
1274
1275 =over 4
1276
1277 =item *
1278
1279 Principle 3: If there are two or more elements in a regexp, the
1280 leftmost greedy (non-greedy) quantifier, if any, will match as much
1281 (little) of the string as possible while still allowing the whole
1282 regexp to match.  The next leftmost greedy (non-greedy) quantifier, if
1283 any, will try to match as much (little) of the string remaining
1284 available to it as possible, while still allowing the whole regexp to
1285 match.  And so on, until all the regexp elements are satisfied.
1286
1287 =back
1288
1289 Just like alternation, quantifiers are also susceptible to
1290 backtracking.  Here is a step-by-step analysis of the example
1291
1292     $x = "the cat in the hat";
1293     $x =~ /^(.*)(at)(.*)$/; # matches,
1294                             # $1 = 'the cat in the h'
1295                             # $2 = 'at'
1296                             # $3 = ''   (0 matches)
1297
1298 =over 4
1299
1300 =item Z<>0.  Start with the first letter in the string C<'t'>.
1301
1302 E<nbsp>
1303
1304 =item Z<>1.  The first quantifier C<'.*'> starts out by matching the whole
1305 string "C<the cat in the hat>".
1306
1307 E<nbsp>
1308
1309 =item Z<>2.  C<'a'> in the regexp element C<'at'> doesn't match the end
1310 of the string.  Backtrack one character.
1311
1312 E<nbsp>
1313
1314 =item Z<>3.  C<'a'> in the regexp element C<'at'> still doesn't match
1315 the last letter of the string C<'t'>, so backtrack one more character.
1316
1317 E<nbsp>
1318
1319 =item Z<>4.  Now we can match the C<'a'> and the C<'t'>.
1320
1321 E<nbsp>
1322
1323 =item Z<>5.  Move on to the third element C<'.*'>.  Since we are at the
1324 end of the string and C<'.*'> can match 0 times, assign it the empty
1325 string.
1326
1327 E<nbsp>
1328
1329 =item Z<>6.  We are done!
1330
1331 =back
1332
1333 Most of the time, all this moving forward and backtracking happens
1334 quickly and searching is fast. There are some pathological regexps,
1335 however, whose execution time exponentially grows with the size of the
1336 string.  A typical structure that blows up in your face is of the form
1337
1338     /(a|b+)*/;
1339
1340 The problem is the nested indeterminate quantifiers.  There are many
1341 different ways of partitioning a string of length n between the C<'+'>
1342 and C<'*'>: one repetition with C<b+> of length n, two repetitions with
1343 the first C<b+> length k and the second with length n-k, m repetitions
1344 whose bits add up to length n, I<etc>.  In fact there are an exponential
1345 number of ways to partition a string as a function of its length.  A
1346 regexp may get lucky and match early in the process, but if there is
1347 no match, Perl will try I<every> possibility before giving up.  So be
1348 careful with nested C<'*'>'s, C<{n,m}>'s, and C<'+'>'s.  The book
1349 I<Mastering Regular Expressions> by Jeffrey Friedl gives a wonderful
1350 discussion of this and other efficiency issues.
1351
1352
1353 =head2 Possessive quantifiers
1354
1355 Backtracking during the relentless search for a match may be a waste
1356 of time, particularly when the match is bound to fail.  Consider
1357 the simple pattern
1358
1359     /^\w+\s+\w+$/; # a word, spaces, a word
1360
1361 Whenever this is applied to a string which doesn't quite meet the
1362 pattern's expectations such as S<C<"abc  ">> or S<C<"abc  def ">>,
1363 the regexp engine will backtrack, approximately once for each character
1364 in the string.  But we know that there is no way around taking I<all>
1365 of the initial word characters to match the first repetition, that I<all>
1366 spaces must be eaten by the middle part, and the same goes for the second
1367 word.
1368
1369 With the introduction of the I<possessive quantifiers> in Perl 5.10, we
1370 have a way of instructing the regexp engine not to backtrack, with the
1371 usual quantifiers with a C<'+'> appended to them.  This makes them greedy as
1372 well as stingy; once they succeed they won't give anything back to permit
1373 another solution. They have the following meanings:
1374
1375 =over 4
1376
1377 =item *
1378
1379 C<a{n,m}+> means: match at least C<n> times, not more than C<m> times,
1380 as many times as possible, and don't give anything up. C<a?+> is short
1381 for C<a{0,1}+>
1382
1383 =item *
1384
1385 C<a{n,}+> means: match at least C<n> times, but as many times as possible,
1386 and don't give anything up. C<a*+> is short for C<a{0,}+> and C<a++> is
1387 short for C<a{1,}+>.
1388
1389 =item *
1390
1391 C<a{n}+> means: match exactly C<n> times.  It is just there for
1392 notational consistency.
1393
1394 =back
1395
1396 These possessive quantifiers represent a special case of a more general
1397 concept, the I<independent subexpression>, see below.
1398
1399 As an example where a possessive quantifier is suitable we consider
1400 matching a quoted string, as it appears in several programming languages.
1401 The backslash is used as an escape character that indicates that the
1402 next character is to be taken literally, as another character for the
1403 string.  Therefore, after the opening quote, we expect a (possibly
1404 empty) sequence of alternatives: either some character except an
1405 unescaped quote or backslash or an escaped character.
1406
1407     /"(?:[^"\\]++|\\.)*+"/;
1408
1409
1410 =head2 Building a regexp
1411
1412 At this point, we have all the basic regexp concepts covered, so let's
1413 give a more involved example of a regular expression.  We will build a
1414 regexp that matches numbers.
1415
1416 The first task in building a regexp is to decide what we want to match
1417 and what we want to exclude.  In our case, we want to match both
1418 integers and floating point numbers and we want to reject any string
1419 that isn't a number.
1420
1421 The next task is to break the problem down into smaller problems that
1422 are easily converted into a regexp.
1423
1424 The simplest case is integers.  These consist of a sequence of digits,
1425 with an optional sign in front.  The digits we can represent with
1426 C<\d+> and the sign can be matched with C<[+-]>.  Thus the integer
1427 regexp is
1428
1429     /[+-]?\d+/;  # matches integers
1430
1431 A floating point number potentially has a sign, an integral part, a
1432 decimal point, a fractional part, and an exponent.  One or more of these
1433 parts is optional, so we need to check out the different
1434 possibilities.  Floating point numbers which are in proper form include
1435 123., 0.345, .34, -1e6, and 25.4E-72.  As with integers, the sign out
1436 front is completely optional and can be matched by C<[+-]?>.  We can
1437 see that if there is no exponent, floating point numbers must have a
1438 decimal point, otherwise they are integers.  We might be tempted to
1439 model these with C<\d*\.\d*>, but this would also match just a single
1440 decimal point, which is not a number.  So the three cases of floating
1441 point number without exponent are
1442
1443    /[+-]?\d+\./;  # 1., 321., etc.
1444    /[+-]?\.\d+/;  # .1, .234, etc.
1445    /[+-]?\d+\.\d+/;  # 1.0, 30.56, etc.
1446
1447 These can be combined into a single regexp with a three-way alternation:
1448
1449    /[+-]?(\d+\.\d+|\d+\.|\.\d+)/;  # floating point, no exponent
1450
1451 In this alternation, it is important to put C<'\d+\.\d+'> before
1452 C<'\d+\.'>.  If C<'\d+\.'> were first, the regexp would happily match that
1453 and ignore the fractional part of the number.
1454
1455 Now consider floating point numbers with exponents.  The key
1456 observation here is that I<both> integers and numbers with decimal
1457 points are allowed in front of an exponent.  Then exponents, like the
1458 overall sign, are independent of whether we are matching numbers with
1459 or without decimal points, and can be "decoupled" from the
1460 mantissa.  The overall form of the regexp now becomes clear:
1461
1462     /^(optional sign)(integer | f.p. mantissa)(optional exponent)$/;
1463
1464 The exponent is an C<'e'> or C<'E'>, followed by an integer.  So the
1465 exponent regexp is
1466
1467    /[eE][+-]?\d+/;  # exponent
1468
1469 Putting all the parts together, we get a regexp that matches numbers:
1470
1471    /^[+-]?(\d+\.\d+|\d+\.|\.\d+|\d+)([eE][+-]?\d+)?$/;  # Ta da!
1472
1473 Long regexps like this may impress your friends, but can be hard to
1474 decipher.  In complex situations like this, the C</x> modifier for a
1475 match is invaluable.  It allows one to put nearly arbitrary whitespace
1476 and comments into a regexp without affecting their meaning.  Using it,
1477 we can rewrite our "extended" regexp in the more pleasing form
1478
1479    /^
1480       [+-]?         # first, match an optional sign
1481       (             # then match integers or f.p. mantissas:
1482           \d+\.\d+  # mantissa of the form a.b
1483          |\d+\.     # mantissa of the form a.
1484          |\.\d+     # mantissa of the form .b
1485          |\d+       # integer of the form a
1486       )
1487       ( [eE] [+-]? \d+ )?  # finally, optionally match an exponent
1488    $/x;
1489
1490 If whitespace is mostly irrelevant, how does one include space
1491 characters in an extended regexp? The answer is to backslash it
1492 S<C<'\ '>> or put it in a character class S<C<[ ]>>.  The same thing
1493 goes for pound signs: use C<\#> or C<[#]>.  For instance, Perl allows
1494 a space between the sign and the mantissa or integer, and we could add
1495 this to our regexp as follows:
1496
1497    /^
1498       [+-]?\ *      # first, match an optional sign *and space*
1499       (             # then match integers or f.p. mantissas:
1500           \d+\.\d+  # mantissa of the form a.b
1501          |\d+\.     # mantissa of the form a.
1502          |\.\d+     # mantissa of the form .b
1503          |\d+       # integer of the form a
1504       )
1505       ( [eE] [+-]? \d+ )?  # finally, optionally match an exponent
1506    $/x;
1507
1508 In this form, it is easier to see a way to simplify the
1509 alternation.  Alternatives 1, 2, and 4 all start with C<\d+>, so it
1510 could be factored out:
1511
1512    /^
1513       [+-]?\ *      # first, match an optional sign
1514       (             # then match integers or f.p. mantissas:
1515           \d+       # start out with a ...
1516           (
1517               \.\d* # mantissa of the form a.b or a.
1518           )?        # ? takes care of integers of the form a
1519          |\.\d+     # mantissa of the form .b
1520       )
1521       ( [eE] [+-]? \d+ )?  # finally, optionally match an exponent
1522    $/x;
1523
1524 Starting in Perl v5.26, specifying C</xx> changes the square-bracketed
1525 portions of a pattern to ignore tabs and space characters unless they
1526 are escaped by preceding them with a backslash.  So, we could write
1527
1528    /^
1529       [ + - ]?\ *   # first, match an optional sign
1530       (             # then match integers or f.p. mantissas:
1531           \d+       # start out with a ...
1532           (
1533               \.\d* # mantissa of the form a.b or a.
1534           )?        # ? takes care of integers of the form a
1535          |\.\d+     # mantissa of the form .b
1536       )
1537       ( [ e E ] [ + - ]? \d+ )?  # finally, optionally match an exponent
1538    $/xx;
1539
1540 This doesn't really improve the legibility of this example, but it's
1541 available in case you want it.  Squashing the pattern down to the
1542 compact form, we have
1543
1544     /^[+-]?\ *(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$/;
1545
1546 This is our final regexp.  To recap, we built a regexp by
1547
1548 =over 4
1549
1550 =item *
1551
1552 specifying the task in detail,
1553
1554 =item *
1555
1556 breaking down the problem into smaller parts,
1557
1558 =item *
1559
1560 translating the small parts into regexps,
1561
1562 =item *
1563
1564 combining the regexps,
1565
1566 =item *
1567
1568 and optimizing the final combined regexp.
1569
1570 =back
1571
1572 These are also the typical steps involved in writing a computer
1573 program.  This makes perfect sense, because regular expressions are
1574 essentially programs written in a little computer language that specifies
1575 patterns.
1576
1577 =head2 Using regular expressions in Perl
1578
1579 The last topic of Part 1 briefly covers how regexps are used in Perl
1580 programs.  Where do they fit into Perl syntax?
1581
1582 We have already introduced the matching operator in its default
1583 C</regexp/> and arbitrary delimiter C<m!regexp!> forms.  We have used
1584 the binding operator C<=~> and its negation C<!~> to test for string
1585 matches.  Associated with the matching operator, we have discussed the
1586 single line C</s>, multi-line C</m>, case-insensitive C</i> and
1587 extended C</x> modifiers.  There are a few more things you might
1588 want to know about matching operators.
1589
1590 =head3 Prohibiting substitution
1591
1592 If you change C<$pattern> after the first substitution happens, Perl
1593 will ignore it.  If you don't want any substitutions at all, use the
1594 special delimiter C<m''>:
1595
1596     @pattern = ('Seuss');
1597     while (<>) {
1598         print if m'@pattern';  # matches literal '@pattern', not 'Seuss'
1599     }
1600
1601 Similar to strings, C<m''> acts like apostrophes on a regexp; all other
1602 C<'m'> delimiters act like quotes.  If the regexp evaluates to the empty string,
1603 the regexp in the I<last successful match> is used instead.  So we have
1604
1605     "dog" =~ /d/;  # 'd' matches
1606     "dogbert =~ //;  # this matches the 'd' regexp used before
1607
1608
1609 =head3 Global matching
1610
1611 The final two modifiers we will discuss here,
1612 C</g> and C</c>, concern multiple matches.
1613 The modifier C</g> stands for global matching and allows the
1614 matching operator to match within a string as many times as possible.
1615 In scalar context, successive invocations against a string will have
1616 C</g> jump from match to match, keeping track of position in the
1617 string as it goes along.  You can get or set the position with the
1618 C<pos()> function.
1619
1620 The use of C</g> is shown in the following example.  Suppose we have
1621 a string that consists of words separated by spaces.  If we know how
1622 many words there are in advance, we could extract the words using
1623 groupings:
1624
1625     $x = "cat dog house"; # 3 words
1626     $x =~ /^\s*(\w+)\s+(\w+)\s+(\w+)\s*$/; # matches,
1627                                            # $1 = 'cat'
1628                                            # $2 = 'dog'
1629                                            # $3 = 'house'
1630
1631 But what if we had an indeterminate number of words? This is the sort
1632 of task C</g> was made for.  To extract all words, form the simple
1633 regexp C<(\w+)> and loop over all matches with C</(\w+)/g>:
1634
1635     while ($x =~ /(\w+)/g) {
1636         print "Word is $1, ends at position ", pos $x, "\n";
1637     }
1638
1639 prints
1640
1641     Word is cat, ends at position 3
1642     Word is dog, ends at position 7
1643     Word is house, ends at position 13
1644
1645 A failed match or changing the target string resets the position.  If
1646 you don't want the position reset after failure to match, add the
1647 C</c>, as in C</regexp/gc>.  The current position in the string is
1648 associated with the string, not the regexp.  This means that different
1649 strings have different positions and their respective positions can be
1650 set or read independently.
1651
1652 In list context, C</g> returns a list of matched groupings, or if
1653 there are no groupings, a list of matches to the whole regexp.  So if
1654 we wanted just the words, we could use
1655
1656     @words = ($x =~ /(\w+)/g);  # matches,
1657                                 # $words[0] = 'cat'
1658                                 # $words[1] = 'dog'
1659                                 # $words[2] = 'house'
1660
1661 Closely associated with the C</g> modifier is the C<\G> anchor.  The
1662 C<\G> anchor matches at the point where the previous C</g> match left
1663 off.  C<\G> allows us to easily do context-sensitive matching:
1664
1665     $metric = 1;  # use metric units
1666     ...
1667     $x = <FILE>;  # read in measurement
1668     $x =~ /^([+-]?\d+)\s*/g;  # get magnitude
1669     $weight = $1;
1670     if ($metric) { # error checking
1671         print "Units error!" unless $x =~ /\Gkg\./g;
1672     }
1673     else {
1674         print "Units error!" unless $x =~ /\Glbs\./g;
1675     }
1676     $x =~ /\G\s+(widget|sprocket)/g;  # continue processing
1677
1678 The combination of C</g> and C<\G> allows us to process the string a
1679 bit at a time and use arbitrary Perl logic to decide what to do next.
1680 Currently, the C<\G> anchor is only fully supported when used to anchor
1681 to the start of the pattern.
1682
1683 C<\G> is also invaluable in processing fixed-length records with
1684 regexps.  Suppose we have a snippet of coding region DNA, encoded as
1685 base pair letters C<ATCGTTGAAT...> and we want to find all the stop
1686 codons C<TGA>.  In a coding region, codons are 3-letter sequences, so
1687 we can think of the DNA snippet as a sequence of 3-letter records.  The
1688 naive regexp
1689
1690     # expanded, this is "ATC GTT GAA TGC AAA TGA CAT GAC"
1691     $dna = "ATCGTTGAATGCAAATGACATGAC";
1692     $dna =~ /TGA/;
1693
1694 doesn't work; it may match a C<TGA>, but there is no guarantee that
1695 the match is aligned with codon boundaries, I<e.g.>, the substring
1696 S<C<GTT GAA>> gives a match.  A better solution is
1697
1698     while ($dna =~ /(\w\w\w)*?TGA/g) {  # note the minimal *?
1699         print "Got a TGA stop codon at position ", pos $dna, "\n";
1700     }
1701
1702 which prints
1703
1704     Got a TGA stop codon at position 18
1705     Got a TGA stop codon at position 23
1706
1707 Position 18 is good, but position 23 is bogus.  What happened?
1708
1709 The answer is that our regexp works well until we get past the last
1710 real match.  Then the regexp will fail to match a synchronized C<TGA>
1711 and start stepping ahead one character position at a time, not what we
1712 want.  The solution is to use C<\G> to anchor the match to the codon
1713 alignment:
1714
1715     while ($dna =~ /\G(\w\w\w)*?TGA/g) {
1716         print "Got a TGA stop codon at position ", pos $dna, "\n";
1717     }
1718
1719 This prints
1720
1721     Got a TGA stop codon at position 18
1722
1723 which is the correct answer.  This example illustrates that it is
1724 important not only to match what is desired, but to reject what is not
1725 desired.
1726
1727 (There are other regexp modifiers that are available, such as
1728 C</o>, but their specialized uses are beyond the
1729 scope of this introduction.  )
1730
1731 =head3 Search and replace
1732
1733 Regular expressions also play a big role in I<search and replace>
1734 operations in Perl.  Search and replace is accomplished with the
1735 C<s///> operator.  The general form is
1736 C<s/regexp/replacement/modifiers>, with everything we know about
1737 regexps and modifiers applying in this case as well.  The
1738 I<replacement> is a Perl double-quoted string that replaces in the
1739 string whatever is matched with the C<regexp>.  The operator C<=~> is
1740 also used here to associate a string with C<s///>.  If matching
1741 against C<$_>, the S<C<$_ =~>> can be dropped.  If there is a match,
1742 C<s///> returns the number of substitutions made; otherwise it returns
1743 false.  Here are a few examples:
1744
1745     $x = "Time to feed the cat!";
1746     $x =~ s/cat/hacker/;   # $x contains "Time to feed the hacker!"
1747     if ($x =~ s/^(Time.*hacker)!$/$1 now!/) {
1748         $more_insistent = 1;
1749     }
1750     $y = "'quoted words'";
1751     $y =~ s/^'(.*)'$/$1/;  # strip single quotes,
1752                            # $y contains "quoted words"
1753
1754 In the last example, the whole string was matched, but only the part
1755 inside the single quotes was grouped.  With the C<s///> operator, the
1756 matched variables C<$1>, C<$2>, I<etc>. are immediately available for use
1757 in the replacement expression, so we use C<$1> to replace the quoted
1758 string with just what was quoted.  With the global modifier, C<s///g>
1759 will search and replace all occurrences of the regexp in the string:
1760
1761     $x = "I batted 4 for 4";
1762     $x =~ s/4/four/;   # doesn't do it all:
1763                        # $x contains "I batted four for 4"
1764     $x = "I batted 4 for 4";
1765     $x =~ s/4/four/g;  # does it all:
1766                        # $x contains "I batted four for four"
1767
1768 If you prefer "regex" over "regexp" in this tutorial, you could use
1769 the following program to replace it:
1770
1771     % cat > simple_replace
1772     #!/usr/bin/perl
1773     $regexp = shift;
1774     $replacement = shift;
1775     while (<>) {
1776         s/$regexp/$replacement/g;
1777         print;
1778     }
1779     ^D
1780
1781     % simple_replace regexp regex perlretut.pod
1782
1783 In C<simple_replace> we used the C<s///g> modifier to replace all
1784 occurrences of the regexp on each line.  (Even though the regular
1785 expression appears in a loop, Perl is smart enough to compile it
1786 only once.)  As with C<simple_grep>, both the
1787 C<print> and the C<s/$regexp/$replacement/g> use C<$_> implicitly.
1788
1789 If you don't want C<s///> to change your original variable you can use
1790 the non-destructive substitute modifier, C<s///r>.  This changes the
1791 behavior so that C<s///r> returns the final substituted string
1792 (instead of the number of substitutions):
1793
1794     $x = "I like dogs.";
1795     $y = $x =~ s/dogs/cats/r;
1796     print "$x $y\n";
1797
1798 That example will print "I like dogs. I like cats". Notice the original
1799 C<$x> variable has not been affected. The overall
1800 result of the substitution is instead stored in C<$y>. If the
1801 substitution doesn't affect anything then the original string is
1802 returned:
1803
1804     $x = "I like dogs.";
1805     $y = $x =~ s/elephants/cougars/r;
1806     print "$x $y\n"; # prints "I like dogs. I like dogs."
1807
1808 One other interesting thing that the C<s///r> flag allows is chaining
1809 substitutions:
1810
1811     $x = "Cats are great.";
1812     print $x =~ s/Cats/Dogs/r =~ s/Dogs/Frogs/r =~
1813         s/Frogs/Hedgehogs/r, "\n";
1814     # prints "Hedgehogs are great."
1815
1816 A modifier available specifically to search and replace is the
1817 C<s///e> evaluation modifier.  C<s///e> treats the
1818 replacement text as Perl code, rather than a double-quoted
1819 string.  The value that the code returns is substituted for the
1820 matched substring.  C<s///e> is useful if you need to do a bit of
1821 computation in the process of replacing text.  This example counts
1822 character frequencies in a line:
1823
1824     $x = "Bill the cat";
1825     $x =~ s/(.)/$chars{$1}++;$1/eg; # final $1 replaces char with itself
1826     print "frequency of '$_' is $chars{$_}\n"
1827         foreach (sort {$chars{$b} <=> $chars{$a}} keys %chars);
1828
1829 This prints
1830
1831     frequency of ' ' is 2
1832     frequency of 't' is 2
1833     frequency of 'l' is 2
1834     frequency of 'B' is 1
1835     frequency of 'c' is 1
1836     frequency of 'e' is 1
1837     frequency of 'h' is 1
1838     frequency of 'i' is 1
1839     frequency of 'a' is 1
1840
1841 As with the match C<m//> operator, C<s///> can use other delimiters,
1842 such as C<s!!!> and C<s{}{}>, and even C<s{}//>.  If single quotes are
1843 used C<s'''>, then the regexp and replacement are
1844 treated as single-quoted strings and there are no
1845 variable substitutions.  C<s///> in list context
1846 returns the same thing as in scalar context, I<i.e.>, the number of
1847 matches.
1848
1849 =head3 The split function
1850
1851 The C<split()> function is another place where a regexp is used.
1852 C<split /regexp/, string, limit> separates the C<string> operand into
1853 a list of substrings and returns that list.  The regexp must be designed
1854 to match whatever constitutes the separators for the desired substrings.
1855 The C<limit>, if present, constrains splitting into no more than C<limit>
1856 number of strings.  For example, to split a string into words, use
1857
1858     $x = "Calvin and Hobbes";
1859     @words = split /\s+/, $x;  # $word[0] = 'Calvin'
1860                                # $word[1] = 'and'
1861                                # $word[2] = 'Hobbes'
1862
1863 If the empty regexp C<//> is used, the regexp always matches and
1864 the string is split into individual characters.  If the regexp has
1865 groupings, then the resulting list contains the matched substrings from the
1866 groupings as well.  For instance,
1867
1868     $x = "/usr/bin/perl";
1869     @dirs = split m!/!, $x;  # $dirs[0] = ''
1870                              # $dirs[1] = 'usr'
1871                              # $dirs[2] = 'bin'
1872                              # $dirs[3] = 'perl'
1873     @parts = split m!(/)!, $x;  # $parts[0] = ''
1874                                 # $parts[1] = '/'
1875                                 # $parts[2] = 'usr'
1876                                 # $parts[3] = '/'
1877                                 # $parts[4] = 'bin'
1878                                 # $parts[5] = '/'
1879                                 # $parts[6] = 'perl'
1880
1881 Since the first character of C<$x> matched the regexp, C<split> prepended
1882 an empty initial element to the list.
1883
1884 If you have read this far, congratulations! You now have all the basic
1885 tools needed to use regular expressions to solve a wide range of text
1886 processing problems.  If this is your first time through the tutorial,
1887 why not stop here and play around with regexps a while....  S<Part 2>
1888 concerns the more esoteric aspects of regular expressions and those
1889 concepts certainly aren't needed right at the start.
1890
1891 =head1 Part 2: Power tools
1892
1893 OK, you know the basics of regexps and you want to know more.  If
1894 matching regular expressions is analogous to a walk in the woods, then
1895 the tools discussed in Part 1 are analogous to topo maps and a
1896 compass, basic tools we use all the time.  Most of the tools in part 2
1897 are analogous to flare guns and satellite phones.  They aren't used
1898 too often on a hike, but when we are stuck, they can be invaluable.
1899
1900 What follows are the more advanced, less used, or sometimes esoteric
1901 capabilities of Perl regexps.  In Part 2, we will assume you are
1902 comfortable with the basics and concentrate on the advanced features.
1903
1904 =head2 More on characters, strings, and character classes
1905
1906 There are a number of escape sequences and character classes that we
1907 haven't covered yet.
1908
1909 There are several escape sequences that convert characters or strings
1910 between upper and lower case, and they are also available within
1911 patterns.  C<\l> and C<\u> convert the next character to lower or
1912 upper case, respectively:
1913
1914     $x = "perl";
1915     $string =~ /\u$x/;  # matches 'Perl' in $string
1916     $x = "M(rs?|s)\\."; # note the double backslash
1917     $string =~ /\l$x/;  # matches 'mr.', 'mrs.', and 'ms.',
1918
1919 A C<\L> or C<\U> indicates a lasting conversion of case, until
1920 terminated by C<\E> or thrown over by another C<\U> or C<\L>:
1921
1922     $x = "This word is in lower case:\L SHOUT\E";
1923     $x =~ /shout/;       # matches
1924     $x = "I STILL KEYPUNCH CARDS FOR MY 360"
1925     $x =~ /\Ukeypunch/;  # matches punch card string
1926
1927 If there is no C<\E>, case is converted until the end of the
1928 string. The regexps C<\L\u$word> or C<\u\L$word> convert the first
1929 character of C<$word> to uppercase and the rest of the characters to
1930 lowercase.
1931
1932 Control characters can be escaped with C<\c>, so that a control-Z
1933 character would be matched with C<\cZ>.  The escape sequence
1934 C<\Q>...C<\E> quotes, or protects most non-alphabetic characters.   For
1935 instance,
1936
1937     $x = "\QThat !^*&%~& cat!";
1938     $x =~ /\Q!^*&%~&\E/;  # check for rough language
1939
1940 It does not protect C<'$'> or C<'@'>, so that variables can still be
1941 substituted.
1942
1943 C<\Q>, C<\L>, C<\l>, C<\U>, C<\u> and C<\E> are actually part of
1944 double-quotish syntax, and not part of regexp syntax proper.  They will
1945 work if they appear in a regular expression embedded directly in a
1946 program, but not when contained in a string that is interpolated in a
1947 pattern.
1948
1949 Perl regexps can handle more than just the
1950 standard ASCII character set.  Perl supports I<Unicode>, a standard
1951 for representing the alphabets from virtually all of the world's written
1952 languages, and a host of symbols.  Perl's text strings are Unicode strings, so
1953 they can contain characters with a value (codepoint or character number) higher
1954 than 255.
1955
1956 What does this mean for regexps? Well, regexp users don't need to know
1957 much about Perl's internal representation of strings.  But they do need
1958 to know 1) how to represent Unicode characters in a regexp and 2) that
1959 a matching operation will treat the string to be searched as a sequence
1960 of characters, not bytes.  The answer to 1) is that Unicode characters
1961 greater than C<chr(255)> are represented using the C<\x{hex}> notation, because
1962 C<\x>I<XY> (without curly braces and I<XY> are two hex digits) doesn't
1963 go further than 255.  (Starting in Perl 5.14, if you're an octal fan,
1964 you can also use C<\o{oct}>.)
1965
1966     /\x{263a}/;  # match a Unicode smiley face :)
1967
1968 B<NOTE>: In Perl 5.6.0 it used to be that one needed to say C<use
1969 utf8> to use any Unicode features.  This is no more the case: for
1970 almost all Unicode processing, the explicit C<utf8> pragma is not
1971 needed.  (The only case where it matters is if your Perl script is in
1972 Unicode and encoded in UTF-8, then an explicit C<use utf8> is needed.)
1973
1974 Figuring out the hexadecimal sequence of a Unicode character you want
1975 or deciphering someone else's hexadecimal Unicode regexp is about as
1976 much fun as programming in machine code.  So another way to specify
1977 Unicode characters is to use the I<named character> escape
1978 sequence C<\N{I<name>}>.  I<name> is a name for the Unicode character, as
1979 specified in the Unicode standard.  For instance, if we wanted to
1980 represent or match the astrological sign for the planet Mercury, we
1981 could use
1982
1983     $x = "abc\N{MERCURY}def";
1984     $x =~ /\N{MERCURY}/;   # matches
1985
1986 One can also use "short" names:
1987
1988     print "\N{GREEK SMALL LETTER SIGMA} is called sigma.\n";
1989     print "\N{greek:Sigma} is an upper-case sigma.\n";
1990
1991 You can also restrict names to a certain alphabet by specifying the
1992 L<charnames> pragma:
1993
1994     use charnames qw(greek);
1995     print "\N{sigma} is Greek sigma\n";
1996
1997 An index of character names is available on-line from the Unicode
1998 Consortium, L<http://www.unicode.org/charts/charindex.html>; explanatory
1999 material with links to other resources at
2000 L<http://www.unicode.org/standard/where>.
2001
2002 The answer to requirement 2) is that a regexp (mostly)
2003 uses Unicode characters.  The "mostly" is for messy backward
2004 compatibility reasons, but starting in Perl 5.14, any regexp compiled in
2005 the scope of a C<use feature 'unicode_strings'> (which is automatically
2006 turned on within the scope of a C<use 5.012> or higher) will turn that
2007 "mostly" into "always".  If you want to handle Unicode properly, you
2008 should ensure that C<'unicode_strings'> is turned on.
2009 Internally, this is encoded to bytes using either UTF-8 or a native 8
2010 bit encoding, depending on the history of the string, but conceptually
2011 it is a sequence of characters, not bytes. See L<perlunitut> for a
2012 tutorial about that.
2013
2014 Let us now discuss Unicode character classes, most usually called
2015 "character properties".  These are represented by the C<\p{I<name>}>
2016 escape sequence.  The negation of this is C<\P{I<name>}>.  For example,
2017 to match lower and uppercase characters,
2018
2019     $x = "BOB";
2020     $x =~ /^\p{IsUpper}/;   # matches, uppercase char class
2021     $x =~ /^\P{IsUpper}/;   # doesn't match, char class sans uppercase
2022     $x =~ /^\p{IsLower}/;   # doesn't match, lowercase char class
2023     $x =~ /^\P{IsLower}/;   # matches, char class sans lowercase
2024
2025 (The "C<Is>" is optional.)
2026
2027 There are many, many Unicode character properties.  For the full list
2028 see L<perluniprops>.  Most of them have synonyms with shorter names,
2029 also listed there.  Some synonyms are a single character.  For these,
2030 you can drop the braces.  For instance, C<\pM> is the same thing as
2031 C<\p{Mark}>, meaning things like accent marks.
2032
2033 The Unicode C<\p{Script}> and C<\p{Script_Extensions}> properties are
2034 used to categorize every Unicode character into the language script it
2035 is written in.  (C<Script_Extensions> is an improved version of
2036 C<Script>, which is retained for backward compatibility, and so you
2037 should generally use C<Script_Extensions>.)
2038 For example,
2039 English, French, and a bunch of other European languages are written in
2040 the Latin script.  But there is also the Greek script, the Thai script,
2041 the Katakana script, I<etc>.  You can test whether a character is in a
2042 particular script (based on C<Script_Extensions>) with, for example
2043 C<\p{Latin}>, C<\p{Greek}>, or C<\p{Katakana}>.  To test if it isn't in
2044 the Balinese script, you would use C<\P{Balinese}>.
2045
2046 What we have described so far is the single form of the C<\p{...}> character
2047 classes.  There is also a compound form which you may run into.  These
2048 look like C<\p{I<name>=I<value>}> or C<\p{I<name>:I<value>}> (the equals sign and colon
2049 can be used interchangeably).  These are more general than the single form,
2050 and in fact most of the single forms are just Perl-defined shortcuts for common
2051 compound forms.  For example, the script examples in the previous paragraph
2052 could be written equivalently as C<\p{Script_Extensions=Latin}>, C<\p{Script_Extensions:Greek}>,
2053 C<\p{script_extensions=katakana}>, and C<\P{script_extensions=balinese}> (case is irrelevant
2054 between the C<{}> braces).  You may
2055 never have to use the compound forms, but sometimes it is necessary, and their
2056 use can make your code easier to understand.
2057
2058 C<\X> is an abbreviation for a character class that comprises
2059 a Unicode I<extended grapheme cluster>.  This represents a "logical character":
2060 what appears to be a single character, but may be represented internally by more
2061 than one.  As an example, using the Unicode full names, I<e.g.>, "S<A + COMBINING
2062 RING>" is a grapheme cluster with base character "A" and combining character
2063 "S<COMBINING RING>, which translates in Danish to "A" with the circle atop it,
2064 as in the word E<Aring>ngstrom.
2065
2066 For the full and latest information about Unicode see the latest
2067 Unicode standard, or the Unicode Consortium's website L<http://www.unicode.org>
2068
2069 As if all those classes weren't enough, Perl also defines POSIX-style
2070 character classes.  These have the form C<[:I<name>:]>, with I<name> the
2071 name of the POSIX class.  The POSIX classes are C<alpha>, C<alnum>,
2072 C<ascii>, C<cntrl>, C<digit>, C<graph>, C<lower>, C<print>, C<punct>,
2073 C<space>, C<upper>, and C<xdigit>, and two extensions, C<word> (a Perl
2074 extension to match C<\w>), and C<blank> (a GNU extension).  The C</a>
2075 modifier restricts these to matching just in the ASCII range; otherwise
2076 they can match the same as their corresponding Perl Unicode classes:
2077 C<[:upper:]> is the same as C<\p{IsUpper}>, I<etc>.  (There are some
2078 exceptions and gotchas with this; see L<perlrecharclass> for a full
2079 discussion.) The C<[:digit:]>, C<[:word:]>, and
2080 C<[:space:]> correspond to the familiar C<\d>, C<\w>, and C<\s>
2081 character classes.  To negate a POSIX class, put a C<'^'> in front of
2082 the name, so that, I<e.g.>, C<[:^digit:]> corresponds to C<\D> and, under
2083 Unicode, C<\P{IsDigit}>.  The Unicode and POSIX character classes can
2084 be used just like C<\d>, with the exception that POSIX character
2085 classes can only be used inside of a character class:
2086
2087     /\s+[abc[:digit:]xyz]\s*/;  # match a,b,c,x,y,z, or a digit
2088     /^=item\s[[:digit:]]/;      # match '=item',
2089                                 # followed by a space and a digit
2090     /\s+[abc\p{IsDigit}xyz]\s+/;  # match a,b,c,x,y,z, or a digit
2091     /^=item\s\p{IsDigit}/;        # match '=item',
2092                                   # followed by a space and a digit
2093
2094 Whew! That is all the rest of the characters and character classes.
2095
2096 =head2 Compiling and saving regular expressions
2097
2098 In Part 1 we mentioned that Perl compiles a regexp into a compact
2099 sequence of opcodes.  Thus, a compiled regexp is a data structure
2100 that can be stored once and used again and again.  The regexp quote
2101 C<qr//> does exactly that: C<qr/string/> compiles the C<string> as a
2102 regexp and transforms the result into a form that can be assigned to a
2103 variable:
2104
2105     $reg = qr/foo+bar?/;  # reg contains a compiled regexp
2106
2107 Then C<$reg> can be used as a regexp:
2108
2109     $x = "fooooba";
2110     $x =~ $reg;     # matches, just like /foo+bar?/
2111     $x =~ /$reg/;   # same thing, alternate form
2112
2113 C<$reg> can also be interpolated into a larger regexp:
2114
2115     $x =~ /(abc)?$reg/;  # still matches
2116
2117 As with the matching operator, the regexp quote can use different
2118 delimiters, I<e.g.>, C<qr!!>, C<qr{}> or C<qr~~>.  Apostrophes
2119 as delimiters (C<qr''>) inhibit any interpolation.
2120
2121 Pre-compiled regexps are useful for creating dynamic matches that
2122 don't need to be recompiled each time they are encountered.  Using
2123 pre-compiled regexps, we write a C<grep_step> program which greps
2124 for a sequence of patterns, advancing to the next pattern as soon
2125 as one has been satisfied.
2126
2127     % cat > grep_step
2128     #!/usr/bin/perl
2129     # grep_step - match <number> regexps, one after the other
2130     # usage: multi_grep <number> regexp1 regexp2 ... file1 file2 ...
2131
2132     $number = shift;
2133     $regexp[$_] = shift foreach (0..$number-1);
2134     @compiled = map qr/$_/, @regexp;
2135     while ($line = <>) {
2136         if ($line =~ /$compiled[0]/) {
2137             print $line;
2138             shift @compiled;
2139             last unless @compiled;
2140         }
2141     }
2142     ^D
2143
2144     % grep_step 3 shift print last grep_step
2145     $number = shift;
2146             print $line;
2147             last unless @compiled;
2148
2149 Storing pre-compiled regexps in an array C<@compiled> allows us to
2150 simply loop through the regexps without any recompilation, thus gaining
2151 flexibility without sacrificing speed.
2152
2153
2154 =head2 Composing regular expressions at runtime
2155
2156 Backtracking is more efficient than repeated tries with different regular
2157 expressions.  If there are several regular expressions and a match with
2158 any of them is acceptable, then it is possible to combine them into a set
2159 of alternatives.  If the individual expressions are input data, this
2160 can be done by programming a join operation.  We'll exploit this idea in
2161 an improved version of the C<simple_grep> program: a program that matches
2162 multiple patterns:
2163
2164     % cat > multi_grep
2165     #!/usr/bin/perl
2166     # multi_grep - match any of <number> regexps
2167     # usage: multi_grep <number> regexp1 regexp2 ... file1 file2 ...
2168
2169     $number = shift;
2170     $regexp[$_] = shift foreach (0..$number-1);
2171     $pattern = join '|', @regexp;
2172
2173     while ($line = <>) {
2174         print $line if $line =~ /$pattern/;
2175     }
2176     ^D
2177
2178     % multi_grep 2 shift for multi_grep
2179     $number = shift;
2180     $regexp[$_] = shift foreach (0..$number-1);
2181
2182 Sometimes it is advantageous to construct a pattern from the I<input>
2183 that is to be analyzed and use the permissible values on the left
2184 hand side of the matching operations.  As an example for this somewhat
2185 paradoxical situation, let's assume that our input contains a command
2186 verb which should match one out of a set of available command verbs,
2187 with the additional twist that commands may be abbreviated as long as
2188 the given string is unique. The program below demonstrates the basic
2189 algorithm.
2190
2191     % cat > keymatch
2192     #!/usr/bin/perl
2193     $kwds = 'copy compare list print';
2194     while( $cmd = <> ){
2195         $cmd =~ s/^\s+|\s+$//g;  # trim leading and trailing spaces
2196         if( ( @matches = $kwds =~ /\b$cmd\w*/g ) == 1 ){
2197             print "command: '@matches'\n";
2198         } elsif( @matches == 0 ){
2199             print "no such command: '$cmd'\n";
2200         } else {
2201             print "not unique: '$cmd' (could be one of: @matches)\n";
2202         }
2203     }
2204     ^D
2205
2206     % keymatch
2207     li
2208     command: 'list'
2209     co
2210     not unique: 'co' (could be one of: copy compare)
2211     printer
2212     no such command: 'printer'
2213
2214 Rather than trying to match the input against the keywords, we match the
2215 combined set of keywords against the input.  The pattern matching
2216 operation S<C<$kwds =~ /\b($cmd\w*)/g>> does several things at the
2217 same time. It makes sure that the given command begins where a keyword
2218 begins (C<\b>). It tolerates abbreviations due to the added C<\w*>. It
2219 tells us the number of matches (C<scalar @matches>) and all the keywords
2220 that were actually matched.  You could hardly ask for more.
2221
2222 =head2 Embedding comments and modifiers in a regular expression
2223
2224 Starting with this section, we will be discussing Perl's set of
2225 I<extended patterns>.  These are extensions to the traditional regular
2226 expression syntax that provide powerful new tools for pattern
2227 matching.  We have already seen extensions in the form of the minimal
2228 matching constructs C<??>, C<*?>, C<+?>, C<{n,m}?>, and C<{n,}?>.  Most
2229 of the extensions below have the form C<(?char...)>, where the
2230 C<char> is a character that determines the type of extension.
2231
2232 The first extension is an embedded comment C<(?#text)>.  This embeds a
2233 comment into the regular expression without affecting its meaning.  The
2234 comment should not have any closing parentheses in the text.  An
2235 example is
2236
2237     /(?# Match an integer:)[+-]?\d+/;
2238
2239 This style of commenting has been largely superseded by the raw,
2240 freeform commenting that is allowed with the C</x> modifier.
2241
2242 Most modifiers, such as C</i>, C</m>, C</s> and C</x> (or any
2243 combination thereof) can also be embedded in
2244 a regexp using C<(?i)>, C<(?m)>, C<(?s)>, and C<(?x)>.  For instance,
2245
2246     /(?i)yes/;  # match 'yes' case insensitively
2247     /yes/i;     # same thing
2248     /(?x)(          # freeform version of an integer regexp
2249              [+-]?  # match an optional sign
2250              \d+    # match a sequence of digits
2251          )
2252     /x;
2253
2254 Embedded modifiers can have two important advantages over the usual
2255 modifiers.  Embedded modifiers allow a custom set of modifiers for
2256 I<each> regexp pattern.  This is great for matching an array of regexps
2257 that must have different modifiers:
2258
2259     $pattern[0] = '(?i)doctor';
2260     $pattern[1] = 'Johnson';
2261     ...
2262     while (<>) {
2263         foreach $patt (@pattern) {
2264             print if /$patt/;
2265         }
2266     }
2267
2268 The second advantage is that embedded modifiers (except C</p>, which
2269 modifies the entire regexp) only affect the regexp
2270 inside the group the embedded modifier is contained in.  So grouping
2271 can be used to localize the modifier's effects:
2272
2273     /Answer: ((?i)yes)/;  # matches 'Answer: yes', 'Answer: YES', etc.
2274
2275 Embedded modifiers can also turn off any modifiers already present
2276 by using, I<e.g.>, C<(?-i)>.  Modifiers can also be combined into
2277 a single expression, I<e.g.>, C<(?s-i)> turns on single line mode and
2278 turns off case insensitivity.
2279
2280 Embedded modifiers may also be added to a non-capturing grouping.
2281 C<(?i-m:regexp)> is a non-capturing grouping that matches C<regexp>
2282 case insensitively and turns off multi-line mode.
2283
2284
2285 =head2 Looking ahead and looking behind
2286
2287 This section concerns the lookahead and lookbehind assertions.  First,
2288 a little background.
2289
2290 In Perl regular expressions, most regexp elements "eat up" a certain
2291 amount of string when they match.  For instance, the regexp element
2292 C<[abc]> eats up one character of the string when it matches, in the
2293 sense that Perl moves to the next character position in the string
2294 after the match.  There are some elements, however, that don't eat up
2295 characters (advance the character position) if they match.  The examples
2296 we have seen so far are the anchors.  The anchor C<'^'> matches the
2297 beginning of the line, but doesn't eat any characters.  Similarly, the
2298 word boundary anchor C<\b> matches wherever a character matching C<\w>
2299 is next to a character that doesn't, but it doesn't eat up any
2300 characters itself.  Anchors are examples of I<zero-width assertions>:
2301 zero-width, because they consume
2302 no characters, and assertions, because they test some property of the
2303 string.  In the context of our walk in the woods analogy to regexp
2304 matching, most regexp elements move us along a trail, but anchors have
2305 us stop a moment and check our surroundings.  If the local environment
2306 checks out, we can proceed forward.  But if the local environment
2307 doesn't satisfy us, we must backtrack.
2308
2309 Checking the environment entails either looking ahead on the trail,
2310 looking behind, or both.  C<'^'> looks behind, to see that there are no
2311 characters before.  C<'$'> looks ahead, to see that there are no
2312 characters after.  C<\b> looks both ahead and behind, to see if the
2313 characters on either side differ in their "word-ness".
2314
2315 The lookahead and lookbehind assertions are generalizations of the
2316 anchor concept.  Lookahead and lookbehind are zero-width assertions
2317 that let us specify which characters we want to test for.  The
2318 lookahead assertion is denoted by C<(?=regexp)> and the lookbehind
2319 assertion is denoted by C<< (?<=fixed-regexp) >>.  Some examples are
2320
2321     $x = "I catch the housecat 'Tom-cat' with catnip";
2322     $x =~ /cat(?=\s)/;   # matches 'cat' in 'housecat'
2323     @catwords = ($x =~ /(?<=\s)cat\w+/g);  # matches,
2324                                            # $catwords[0] = 'catch'
2325                                            # $catwords[1] = 'catnip'
2326     $x =~ /\bcat\b/;  # matches 'cat' in 'Tom-cat'
2327     $x =~ /(?<=\s)cat(?=\s)/; # doesn't match; no isolated 'cat' in
2328                               # middle of $x
2329
2330 Note that the parentheses in C<(?=regexp)> and C<< (?<=regexp) >> are
2331 non-capturing, since these are zero-width assertions.  Thus in the
2332 second regexp, the substrings captured are those of the whole regexp
2333 itself.  Lookahead C<(?=regexp)> can match arbitrary regexps, but
2334 lookbehind C<< (?<=fixed-regexp) >> only works for regexps of fixed
2335 width, I<i.e.>, a fixed number of characters long.  Thus
2336 C<< (?<=(ab|bc)) >> is fine, but C<< (?<=(ab)*) >> is not.  The
2337 negated versions of the lookahead and lookbehind assertions are
2338 denoted by C<(?!regexp)> and C<< (?<!fixed-regexp) >> respectively.
2339 They evaluate true if the regexps do I<not> match:
2340
2341     $x = "foobar";
2342     $x =~ /foo(?!bar)/;  # doesn't match, 'bar' follows 'foo'
2343     $x =~ /foo(?!baz)/;  # matches, 'baz' doesn't follow 'foo'
2344     $x =~ /(?<!\s)foo/;  # matches, there is no \s before 'foo'
2345
2346 Here is an example where a string containing blank-separated words,
2347 numbers and single dashes is to be split into its components.
2348 Using C</\s+/> alone won't work, because spaces are not required between
2349 dashes, or a word or a dash. Additional places for a split are established
2350 by looking ahead and behind:
2351
2352     $str = "one two - --6-8";
2353     @toks = split / \s+              # a run of spaces
2354                   | (?<=\S) (?=-)    # any non-space followed by '-'
2355                   | (?<=-)  (?=\S)   # a '-' followed by any non-space
2356                   /x, $str;          # @toks = qw(one two - - - 6 - 8)
2357
2358
2359 =head2 Using independent subexpressions to prevent backtracking
2360
2361 I<Independent subexpressions> are regular expressions, in the
2362 context of a larger regular expression, that function independently of
2363 the larger regular expression.  That is, they consume as much or as
2364 little of the string as they wish without regard for the ability of
2365 the larger regexp to match.  Independent subexpressions are represented
2366 by C<< (?>regexp) >>.  We can illustrate their behavior by first
2367 considering an ordinary regexp:
2368
2369     $x = "ab";
2370     $x =~ /a*ab/;  # matches
2371
2372 This obviously matches, but in the process of matching, the
2373 subexpression C<a*> first grabbed the C<'a'>.  Doing so, however,
2374 wouldn't allow the whole regexp to match, so after backtracking, C<a*>
2375 eventually gave back the C<'a'> and matched the empty string.  Here, what
2376 C<a*> matched was I<dependent> on what the rest of the regexp matched.
2377
2378 Contrast that with an independent subexpression:
2379
2380     $x =~ /(?>a*)ab/;  # doesn't match!
2381
2382 The independent subexpression C<< (?>a*) >> doesn't care about the rest
2383 of the regexp, so it sees an C<'a'> and grabs it.  Then the rest of the
2384 regexp C<ab> cannot match.  Because C<< (?>a*) >> is independent, there
2385 is no backtracking and the independent subexpression does not give
2386 up its C<'a'>.  Thus the match of the regexp as a whole fails.  A similar
2387 behavior occurs with completely independent regexps:
2388
2389     $x = "ab";
2390     $x =~ /a*/g;   # matches, eats an 'a'
2391     $x =~ /\Gab/g; # doesn't match, no 'a' available
2392
2393 Here C</g> and C<\G> create a "tag team" handoff of the string from
2394 one regexp to the other.  Regexps with an independent subexpression are
2395 much like this, with a handoff of the string to the independent
2396 subexpression, and a handoff of the string back to the enclosing
2397 regexp.
2398
2399 The ability of an independent subexpression to prevent backtracking
2400 can be quite useful.  Suppose we want to match a non-empty string
2401 enclosed in parentheses up to two levels deep.  Then the following
2402 regexp matches:
2403
2404     $x = "abc(de(fg)h";  # unbalanced parentheses
2405     $x =~ /\( ( [ ^ () ]+ | \( [ ^ () ]* \) )+ \)/xx;
2406
2407 The regexp matches an open parenthesis, one or more copies of an
2408 alternation, and a close parenthesis.  The alternation is two-way, with
2409 the first alternative C<[^()]+> matching a substring with no
2410 parentheses and the second alternative C<\([^()]*\)>  matching a
2411 substring delimited by parentheses.  The problem with this regexp is
2412 that it is pathological: it has nested indeterminate quantifiers
2413 of the form C<(a+|b)+>.  We discussed in Part 1 how nested quantifiers
2414 like this could take an exponentially long time to execute if there
2415 was no match possible.  To prevent the exponential blowup, we need to
2416 prevent useless backtracking at some point.  This can be done by
2417 enclosing the inner quantifier as an independent subexpression:
2418
2419     $x =~ /\( ( (?> [ ^ () ]+ ) | \([ ^ () ]* \) )+ \)/xx;
2420
2421 Here, C<< (?>[^()]+) >> breaks the degeneracy of string partitioning
2422 by gobbling up as much of the string as possible and keeping it.   Then
2423 match failures fail much more quickly.
2424
2425
2426 =head2 Conditional expressions
2427
2428 A I<conditional expression> is a form of if-then-else statement
2429 that allows one to choose which patterns are to be matched, based on
2430 some condition.  There are two types of conditional expression:
2431 C<(?(I<condition>)I<yes-regexp>)> and
2432 C<(?(condition)I<yes-regexp>|I<no-regexp>)>.
2433 C<(?(I<condition>)I<yes-regexp>)> is
2434 like an S<C<'if () {}'>> statement in Perl.  If the I<condition> is true,
2435 the I<yes-regexp> will be matched.  If the I<condition> is false, the
2436 I<yes-regexp> will be skipped and Perl will move onto the next regexp
2437 element.  The second form is like an S<C<'if () {} else {}'>> statement
2438 in Perl.  If the I<condition> is true, the I<yes-regexp> will be
2439 matched, otherwise the I<no-regexp> will be matched.
2440
2441 The I<condition> can have several forms.  The first form is simply an
2442 integer in parentheses C<(I<integer>)>.  It is true if the corresponding
2443 backreference C<\I<integer>> matched earlier in the regexp.  The same
2444 thing can be done with a name associated with a capture group, written
2445 as C<<< (E<lt>I<name>E<gt>) >>> or C<< ('I<name>') >>.  The second form is a bare
2446 zero-width assertion C<(?...)>, either a lookahead, a lookbehind, or a
2447 code assertion (discussed in the next section).  The third set of forms
2448 provides tests that return true if the expression is executed within
2449 a recursion (C<(R)>) or is being called from some capturing group,
2450 referenced either by number (C<(R1)>, C<(R2)>,...) or by name
2451 (C<(R&I<name>)>).
2452
2453 The integer or name form of the C<condition> allows us to choose,
2454 with more flexibility, what to match based on what matched earlier in the
2455 regexp. This searches for words of the form C<"$x$x"> or C<"$x$y$y$x">:
2456
2457     % simple_grep '^(\w+)(\w+)?(?(2)\g2\g1|\g1)$' /usr/dict/words
2458     beriberi
2459     coco
2460     couscous
2461     deed
2462     ...
2463     toot
2464     toto
2465     tutu
2466
2467 The lookbehind C<condition> allows, along with backreferences,
2468 an earlier part of the match to influence a later part of the
2469 match.  For instance,
2470
2471     /[ATGC]+(?(?<=AA)G|C)$/;
2472
2473 matches a DNA sequence such that it either ends in C<AAG>, or some
2474 other base pair combination and C<'C'>.  Note that the form is
2475 C<< (?(?<=AA)G|C) >> and not C<< (?((?<=AA))G|C) >>; for the
2476 lookahead, lookbehind or code assertions, the parentheses around the
2477 conditional are not needed.
2478
2479
2480 =head2 Defining named patterns
2481
2482 Some regular expressions use identical subpatterns in several places.
2483 Starting with Perl 5.10, it is possible to define named subpatterns in
2484 a section of the pattern so that they can be called up by name
2485 anywhere in the pattern.  This syntactic pattern for this definition
2486 group is C<< (?(DEFINE)(?<I<name>>I<pattern>)...) >>.  An insertion
2487 of a named pattern is written as C<(?&I<name>)>.
2488
2489 The example below illustrates this feature using the pattern for
2490 floating point numbers that was presented earlier on.  The three
2491 subpatterns that are used more than once are the optional sign, the
2492 digit sequence for an integer and the decimal fraction.  The C<DEFINE>
2493 group at the end of the pattern contains their definition.  Notice
2494 that the decimal fraction pattern is the first place where we can
2495 reuse the integer pattern.
2496
2497    /^ (?&osg)\ * ( (?&int)(?&dec)? | (?&dec) )
2498       (?: [eE](?&osg)(?&int) )?
2499     $
2500     (?(DEFINE)
2501       (?<osg>[-+]?)         # optional sign
2502       (?<int>\d++)          # integer
2503       (?<dec>\.(?&int))     # decimal fraction
2504     )/x
2505
2506
2507 =head2 Recursive patterns
2508
2509 This feature (introduced in Perl 5.10) significantly extends the
2510 power of Perl's pattern matching.  By referring to some other
2511 capture group anywhere in the pattern with the construct
2512 C<(?I<group-ref>)>, the I<pattern> within the referenced group is used
2513 as an independent subpattern in place of the group reference itself.
2514 Because the group reference may be contained I<within> the group it
2515 refers to, it is now possible to apply pattern matching to tasks that
2516 hitherto required a recursive parser.
2517
2518 To illustrate this feature, we'll design a pattern that matches if
2519 a string contains a palindrome. (This is a word or a sentence that,
2520 while ignoring spaces, interpunctuation and case, reads the same backwards
2521 as forwards. We begin by observing that the empty string or a string
2522 containing just one word character is a palindrome. Otherwise it must
2523 have a word character up front and the same at its end, with another
2524 palindrome in between.
2525
2526     /(?: (\w) (?...Here be a palindrome...) \g{-1} | \w? )/x
2527
2528 Adding C<\W*> at either end to eliminate what is to be ignored, we already
2529 have the full pattern:
2530
2531     my $pp = qr/^(\W* (?: (\w) (?1) \g{-1} | \w? ) \W*)$/ix;
2532     for $s ( "saippuakauppias", "A man, a plan, a canal: Panama!" ){
2533         print "'$s' is a palindrome\n" if $s =~ /$pp/;
2534     }
2535
2536 In C<(?...)> both absolute and relative backreferences may be used.
2537 The entire pattern can be reinserted with C<(?R)> or C<(?0)>.
2538 If you prefer to name your groups, you can use C<(?&I<name>)> to
2539 recurse into that group.
2540
2541
2542 =head2 A bit of magic: executing Perl code in a regular expression
2543
2544 Normally, regexps are a part of Perl expressions.
2545 I<Code evaluation> expressions turn that around by allowing
2546 arbitrary Perl code to be a part of a regexp.  A code evaluation
2547 expression is denoted C<(?{I<code>})>, with I<code> a string of Perl
2548 statements.
2549
2550 Code expressions are zero-width assertions, and the value they return
2551 depends on their environment.  There are two possibilities: either the
2552 code expression is used as a conditional in a conditional expression
2553 C<(?(I<condition>)...)>, or it is not.  If the code expression is a
2554 conditional, the code is evaluated and the result (I<i.e.>, the result of
2555 the last statement) is used to determine truth or falsehood.  If the
2556 code expression is not used as a conditional, the assertion always
2557 evaluates true and the result is put into the special variable
2558 C<$^R>.  The variable C<$^R> can then be used in code expressions later
2559 in the regexp.  Here are some silly examples:
2560
2561     $x = "abcdef";
2562     $x =~ /abc(?{print "Hi Mom!";})def/; # matches,
2563                                          # prints 'Hi Mom!'
2564     $x =~ /aaa(?{print "Hi Mom!";})def/; # doesn't match,
2565                                          # no 'Hi Mom!'
2566
2567 Pay careful attention to the next example:
2568
2569     $x =~ /abc(?{print "Hi Mom!";})ddd/; # doesn't match,
2570                                          # no 'Hi Mom!'
2571                                          # but why not?
2572
2573 At first glance, you'd think that it shouldn't print, because obviously
2574 the C<ddd> isn't going to match the target string. But look at this
2575 example:
2576
2577     $x =~ /abc(?{print "Hi Mom!";})[dD]dd/; # doesn't match,
2578                                             # but _does_ print
2579
2580 Hmm. What happened here? If you've been following along, you know that
2581 the above pattern should be effectively (almost) the same as the last one;
2582 enclosing the C<'d'> in a character class isn't going to change what it
2583 matches. So why does the first not print while the second one does?
2584
2585 The answer lies in the optimizations the regexp engine makes. In the first
2586 case, all the engine sees are plain old characters (aside from the
2587 C<?{}> construct). It's smart enough to realize that the string C<'ddd'>
2588 doesn't occur in our target string before actually running the pattern
2589 through. But in the second case, we've tricked it into thinking that our
2590 pattern is more complicated. It takes a look, sees our
2591 character class, and decides that it will have to actually run the
2592 pattern to determine whether or not it matches, and in the process of
2593 running it hits the print statement before it discovers that we don't
2594 have a match.
2595
2596 To take a closer look at how the engine does optimizations, see the
2597 section L</"Pragmas and debugging"> below.
2598
2599 More fun with C<?{}>:
2600
2601     $x =~ /(?{print "Hi Mom!";})/;       # matches,
2602                                          # prints 'Hi Mom!'
2603     $x =~ /(?{$c = 1;})(?{print "$c";})/;  # matches,
2604                                            # prints '1'
2605     $x =~ /(?{$c = 1;})(?{print "$^R";})/; # matches,
2606                                            # prints '1'
2607
2608 The bit of magic mentioned in the section title occurs when the regexp
2609 backtracks in the process of searching for a match.  If the regexp
2610 backtracks over a code expression and if the variables used within are
2611 localized using C<local>, the changes in the variables produced by the
2612 code expression are undone! Thus, if we wanted to count how many times
2613 a character got matched inside a group, we could use, I<e.g.>,
2614
2615     $x = "aaaa";
2616     $count = 0;  # initialize 'a' count
2617     $c = "bob";  # test if $c gets clobbered
2618     $x =~ /(?{local $c = 0;})         # initialize count
2619            ( a                        # match 'a'
2620              (?{local $c = $c + 1;})  # increment count
2621            )*                         # do this any number of times,
2622            aa                         # but match 'aa' at the end
2623            (?{$count = $c;})          # copy local $c var into $count
2624           /x;
2625     print "'a' count is $count, \$c variable is '$c'\n";
2626
2627 This prints
2628
2629     'a' count is 2, $c variable is 'bob'
2630
2631 If we replace the S<C< (?{local $c = $c + 1;})>> with
2632 S<C< (?{$c = $c + 1;})>>, the variable changes are I<not> undone
2633 during backtracking, and we get
2634
2635     'a' count is 4, $c variable is 'bob'
2636
2637 Note that only localized variable changes are undone.  Other side
2638 effects of code expression execution are permanent.  Thus
2639
2640     $x = "aaaa";
2641     $x =~ /(a(?{print "Yow\n";}))*aa/;
2642
2643 produces
2644
2645    Yow
2646    Yow
2647    Yow
2648    Yow
2649
2650 The result C<$^R> is automatically localized, so that it will behave
2651 properly in the presence of backtracking.
2652
2653 This example uses a code expression in a conditional to match a
2654 definite article, either C<'the'> in English or C<'der|die|das'> in
2655 German:
2656
2657     $lang = 'DE';  # use German
2658     ...
2659     $text = "das";
2660     print "matched\n"
2661         if $text =~ /(?(?{
2662                           $lang eq 'EN'; # is the language English?
2663                          })
2664                        the |             # if so, then match 'the'
2665                        (der|die|das)     # else, match 'der|die|das'
2666                      )
2667                     /xi;
2668
2669 Note that the syntax here is C<(?(?{...})I<yes-regexp>|I<no-regexp>)>, not
2670 C<(?((?{...}))I<yes-regexp>|I<no-regexp>)>.  In other words, in the case of a
2671 code expression, we don't need the extra parentheses around the
2672 conditional.
2673
2674 If you try to use code expressions where the code text is contained within
2675 an interpolated variable, rather than appearing literally in the pattern,
2676 Perl may surprise you:
2677
2678     $bar = 5;
2679     $pat = '(?{ 1 })';
2680     /foo(?{ $bar })bar/; # compiles ok, $bar not interpolated
2681     /foo(?{ 1 })$bar/;   # compiles ok, $bar interpolated
2682     /foo${pat}bar/;      # compile error!
2683
2684     $pat = qr/(?{ $foo = 1 })/;  # precompile code regexp
2685     /foo${pat}bar/;      # compiles ok
2686
2687 If a regexp has a variable that interpolates a code expression, Perl
2688 treats the regexp as an error. If the code expression is precompiled into
2689 a variable, however, interpolating is ok. The question is, why is this an
2690 error?
2691
2692 The reason is that variable interpolation and code expressions
2693 together pose a security risk.  The combination is dangerous because
2694 many programmers who write search engines often take user input and
2695 plug it directly into a regexp:
2696
2697     $regexp = <>;       # read user-supplied regexp
2698     $chomp $regexp;     # get rid of possible newline
2699     $text =~ /$regexp/; # search $text for the $regexp
2700
2701 If the C<$regexp> variable contains a code expression, the user could
2702 then execute arbitrary Perl code.  For instance, some joker could
2703 search for S<C<system('rm -rf *');>> to erase your files.  In this
2704 sense, the combination of interpolation and code expressions I<taints>
2705 your regexp.  So by default, using both interpolation and code
2706 expressions in the same regexp is not allowed.  If you're not
2707 concerned about malicious users, it is possible to bypass this
2708 security check by invoking S<C<use re 'eval'>>:
2709
2710     use re 'eval';       # throw caution out the door
2711     $bar = 5;
2712     $pat = '(?{ 1 })';
2713     /foo${pat}bar/;      # compiles ok
2714
2715 Another form of code expression is the I<pattern code expression>.
2716 The pattern code expression is like a regular code expression, except
2717 that the result of the code evaluation is treated as a regular
2718 expression and matched immediately.  A simple example is
2719
2720     $length = 5;
2721     $char = 'a';
2722     $x = 'aaaaabb';
2723     $x =~ /(??{$char x $length})/x; # matches, there are 5 of 'a'
2724
2725
2726 This final example contains both ordinary and pattern code
2727 expressions.  It detects whether a binary string C<1101010010001...> has a
2728 Fibonacci spacing 0,1,1,2,3,5,...  of the C<'1'>'s:
2729
2730     $x = "1101010010001000001";
2731     $z0 = ''; $z1 = '0';   # initial conditions
2732     print "It is a Fibonacci sequence\n"
2733         if $x =~ /^1         # match an initial '1'
2734                     (?:
2735                        ((??{ $z0 })) # match some '0'
2736                        1             # and then a '1'
2737                        (?{ $z0 = $z1; $z1 .= $^N; })
2738                     )+   # repeat as needed
2739                   $      # that is all there is
2740                  /x;
2741     printf "Largest sequence matched was %d\n", length($z1)-length($z0);
2742
2743 Remember that C<$^N> is set to whatever was matched by the last
2744 completed capture group. This prints
2745
2746     It is a Fibonacci sequence
2747     Largest sequence matched was 5
2748
2749 Ha! Try that with your garden variety regexp package...
2750
2751 Note that the variables C<$z0> and C<$z1> are not substituted when the
2752 regexp is compiled, as happens for ordinary variables outside a code
2753 expression.  Rather, the whole code block is parsed as perl code at the
2754 same time as perl is compiling the code containing the literal regexp
2755 pattern.
2756
2757 This regexp without the C</x> modifier is
2758
2759     /^1(?:((??{ $z0 }))1(?{ $z0 = $z1; $z1 .= $^N; }))+$/
2760
2761 which shows that spaces are still possible in the code parts. Nevertheless,
2762 when working with code and conditional expressions, the extended form of
2763 regexps is almost necessary in creating and debugging regexps.
2764
2765
2766 =head2 Backtracking control verbs
2767
2768 Perl 5.10 introduced a number of control verbs intended to provide
2769 detailed control over the backtracking process, by directly influencing
2770 the regexp engine and by providing monitoring techniques.  See
2771 L<perlre/"Special Backtracking Control Verbs"> for a detailed
2772 description.
2773
2774 Below is just one example, illustrating the control verb C<(*FAIL)>,
2775 which may be abbreviated as C<(*F)>. If this is inserted in a regexp
2776 it will cause it to fail, just as it would at some
2777 mismatch between the pattern and the string. Processing
2778 of the regexp continues as it would after any "normal"
2779 failure, so that, for instance, the next position in the string or another
2780 alternative will be tried. As failing to match doesn't preserve capture
2781 groups or produce results, it may be necessary to use this in
2782 combination with embedded code.
2783
2784    %count = ();
2785    "supercalifragilisticexpialidocious" =~
2786        /([aeiou])(?{ $count{$1}++; })(*FAIL)/i;
2787    printf "%3d '%s'\n", $count{$_}, $_ for (sort keys %count);
2788
2789 The pattern begins with a class matching a subset of letters.  Whenever
2790 this matches, a statement like C<$count{'a'}++;> is executed, incrementing
2791 the letter's counter. Then C<(*FAIL)> does what it says, and
2792 the regexp engine proceeds according to the book: as long as the end of
2793 the string hasn't been reached, the position is advanced before looking
2794 for another vowel. Thus, match or no match makes no difference, and the
2795 regexp engine proceeds until the entire string has been inspected.
2796 (It's remarkable that an alternative solution using something like
2797
2798    $count{lc($_)}++ for split('', "supercalifragilisticexpialidocious");
2799    printf "%3d '%s'\n", $count2{$_}, $_ for ( qw{ a e i o u } );
2800
2801 is considerably slower.)
2802
2803
2804 =head2 Pragmas and debugging
2805
2806 Speaking of debugging, there are several pragmas available to control
2807 and debug regexps in Perl.  We have already encountered one pragma in
2808 the previous section, S<C<use re 'eval';>>, that allows variable
2809 interpolation and code expressions to coexist in a regexp.  The other
2810 pragmas are
2811
2812     use re 'taint';
2813     $tainted = <>;
2814     @parts = ($tainted =~ /(\w+)\s+(\w+)/; # @parts is now tainted
2815
2816 The C<taint> pragma causes any substrings from a match with a tainted
2817 variable to be tainted as well.  This is not normally the case, as
2818 regexps are often used to extract the safe bits from a tainted
2819 variable.  Use C<taint> when you are not extracting safe bits, but are
2820 performing some other processing.  Both C<taint> and C<eval> pragmas
2821 are lexically scoped, which means they are in effect only until
2822 the end of the block enclosing the pragmas.
2823
2824     use re '/m';  # or any other flags
2825     $multiline_string =~ /^foo/; # /m is implied
2826
2827 The C<re '/flags'> pragma (introduced in Perl
2828 5.14) turns on the given regular expression flags
2829 until the end of the lexical scope.  See
2830 L<re/"'E<sol>flags' mode"> for more
2831 detail.
2832
2833     use re 'debug';
2834     /^(.*)$/s;       # output debugging info
2835
2836     use re 'debugcolor';
2837     /^(.*)$/s;       # output debugging info in living color
2838
2839 The global C<debug> and C<debugcolor> pragmas allow one to get
2840 detailed debugging info about regexp compilation and
2841 execution.  C<debugcolor> is the same as debug, except the debugging
2842 information is displayed in color on terminals that can display
2843 termcap color sequences.  Here is example output:
2844
2845     % perl -e 'use re "debug"; "abc" =~ /a*b+c/;'
2846     Compiling REx 'a*b+c'
2847     size 9 first at 1
2848        1: STAR(4)
2849        2:   EXACT <a>(0)
2850        4: PLUS(7)
2851        5:   EXACT <b>(0)
2852        7: EXACT <c>(9)
2853        9: END(0)
2854     floating 'bc' at 0..2147483647 (checking floating) minlen 2
2855     Guessing start of match, REx 'a*b+c' against 'abc'...
2856     Found floating substr 'bc' at offset 1...
2857     Guessed: match at offset 0
2858     Matching REx 'a*b+c' against 'abc'
2859       Setting an EVAL scope, savestack=3
2860        0 <> <abc>           |  1:  STAR
2861                              EXACT <a> can match 1 times out of 32767...
2862       Setting an EVAL scope, savestack=3
2863        1 <a> <bc>           |  4:    PLUS
2864                              EXACT <b> can match 1 times out of 32767...
2865       Setting an EVAL scope, savestack=3
2866        2 <ab> <c>           |  7:      EXACT <c>
2867        3 <abc> <>           |  9:      END
2868     Match successful!
2869     Freeing REx: 'a*b+c'
2870
2871 If you have gotten this far into the tutorial, you can probably guess
2872 what the different parts of the debugging output tell you.  The first
2873 part
2874
2875     Compiling REx 'a*b+c'
2876     size 9 first at 1
2877        1: STAR(4)
2878        2:   EXACT <a>(0)
2879        4: PLUS(7)
2880        5:   EXACT <b>(0)
2881        7: EXACT <c>(9)
2882        9: END(0)
2883
2884 describes the compilation stage.  C<STAR(4)> means that there is a
2885 starred object, in this case C<'a'>, and if it matches, goto line 4,
2886 I<i.e.>, C<PLUS(7)>.  The middle lines describe some heuristics and
2887 optimizations performed before a match:
2888
2889     floating 'bc' at 0..2147483647 (checking floating) minlen 2
2890     Guessing start of match, REx 'a*b+c' against 'abc'...
2891     Found floating substr 'bc' at offset 1...
2892     Guessed: match at offset 0
2893
2894 Then the match is executed and the remaining lines describe the
2895 process:
2896
2897     Matching REx 'a*b+c' against 'abc'
2898       Setting an EVAL scope, savestack=3
2899        0 <> <abc>           |  1:  STAR
2900                              EXACT <a> can match 1 times out of 32767...
2901       Setting an EVAL scope, savestack=3
2902        1 <a> <bc>           |  4:    PLUS
2903                              EXACT <b> can match 1 times out of 32767...
2904       Setting an EVAL scope, savestack=3
2905        2 <ab> <c>           |  7:      EXACT <c>
2906        3 <abc> <>           |  9:      END
2907     Match successful!
2908     Freeing REx: 'a*b+c'
2909
2910 Each step is of the form S<C<< n <x> <y> >>>, with C<< <x> >> the
2911 part of the string matched and C<< <y> >> the part not yet
2912 matched.  The S<C<< |  1:  STAR >>> says that Perl is at line number 1
2913 in the compilation list above.  See
2914 L<perldebguts/"Debugging Regular Expressions"> for much more detail.
2915
2916 An alternative method of debugging regexps is to embed C<print>
2917 statements within the regexp.  This provides a blow-by-blow account of
2918 the backtracking in an alternation:
2919
2920     "that this" =~ m@(?{print "Start at position ", pos, "\n";})
2921                      t(?{print "t1\n";})
2922                      h(?{print "h1\n";})
2923                      i(?{print "i1\n";})
2924                      s(?{print "s1\n";})
2925                          |
2926                      t(?{print "t2\n";})
2927                      h(?{print "h2\n";})
2928                      a(?{print "a2\n";})
2929                      t(?{print "t2\n";})
2930                      (?{print "Done at position ", pos, "\n";})
2931                     @x;
2932
2933 prints
2934
2935     Start at position 0
2936     t1
2937     h1
2938     t2
2939     h2
2940     a2
2941     t2
2942     Done at position 4
2943
2944 =head1 SEE ALSO
2945
2946 This is just a tutorial.  For the full story on Perl regular
2947 expressions, see the L<perlre> regular expressions reference page.
2948
2949 For more information on the matching C<m//> and substitution C<s///>
2950 operators, see L<perlop/"Regexp Quote-Like Operators">.  For
2951 information on the C<split> operation, see L<perlfunc/split>.
2952
2953 For an excellent all-around resource on the care and feeding of
2954 regular expressions, see the book I<Mastering Regular Expressions> by
2955 Jeffrey Friedl (published by O'Reilly, ISBN 1556592-257-3).
2956
2957 =head1 AUTHOR AND COPYRIGHT
2958
2959 Copyright (c) 2000 Mark Kvale.
2960 All rights reserved.
2961 Now maintained by Perl porters.
2962
2963 This document may be distributed under the same terms as Perl itself.
2964
2965 =head2 Acknowledgments
2966
2967 The inspiration for the stop codon DNA example came from the ZIP
2968 code example in chapter 7 of I<Mastering Regular Expressions>.
2969
2970 The author would like to thank Jeff Pinyan, Andrew Johnson, Peter
2971 Haworth, Ronald J Kimball, and Joe Smith for all their helpful
2972 comments.
2973
2974 =cut
2975