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