Make the large file tests more robust/talkative as suggested by
[p5sagit/p5-mst-13.2.git] / pod / perlrequick.pod
CommitLineData
47f9c88b 1=head1 NAME
2
3perlrequick - Perl regular expressions quick start
4
5=head1 DESCRIPTION
6
7This page covers the very basics of understanding, creating and
6425a278 8using regular expressions ('regexes') in Perl.
9
47f9c88b 10
11=head1 The Guide
12
13=head2 Simple word matching
14
6425a278 15The simplest regex is simply a word, or more generally, a string of
16characters. A regex consisting of a word matches any string that
47f9c88b 17contains that word:
18
19 "Hello World" =~ /World/; # matches
20
6425a278 21In this statement, C<World> is a regex and the C<//> enclosing
47f9c88b 22C</World/> tells perl to search a string for a match. The operator
6425a278 23C<=~> associates the string with the regex match and produces a true
24value if the regex matched, or false if the regex did not match. In
47f9c88b 25our case, C<World> matches the second word in C<"Hello World">, so the
26expression is true. This idea has several variations.
27
28Expressions like this are useful in conditionals:
29
30 print "It matches\n" if "Hello World" =~ /World/;
31
32The sense of the match can be reversed by using C<!~> operator:
33
34 print "It doesn't match\n" if "Hello World" !~ /World/;
35
6425a278 36The literal string in the regex can be replaced by a variable:
47f9c88b 37
38 $greeting = "World";
39 print "It matches\n" if "Hello World" =~ /$greeting/;
40
41If you're matching against C<$_>, the C<$_ =~> part can be omitted:
42
43 $_ = "Hello World";
44 print "It matches\n" if /World/;
45
46Finally, the C<//> default delimiters for a match can be changed to
47arbitrary delimiters by putting an C<'m'> out front:
48
49 "Hello World" =~ m!World!; # matches, delimited by '!'
50 "Hello World" =~ m{World}; # matches, note the matching '{}'
51 "/usr/bin/perl" =~ m"/perl"; # matches after '/usr/bin',
52 # '/' becomes an ordinary char
53
6425a278 54Regexes must match a part of the string I<exactly> in order for the
47f9c88b 55statement to be true:
56
57 "Hello World" =~ /world/; # doesn't match, case sensitive
58 "Hello World" =~ /o W/; # matches, ' ' is an ordinary char
59 "Hello World" =~ /World /; # doesn't match, no ' ' at end
60
61perl will always match at the earliest possible point in the string:
62
63 "Hello World" =~ /o/; # matches 'o' in 'Hello'
64 "That hat is red" =~ /hat/; # matches 'hat' in 'That'
65
66Not all characters can be used 'as is' in a match. Some characters,
6425a278 67called B<metacharacters>, are reserved for use in regex notation.
47f9c88b 68The metacharacters are
69
70 {}[]()^$.|*+?\
71
72A metacharacter can be matched by putting a backslash before it:
73
74 "2+2=4" =~ /2+2/; # doesn't match, + is a metacharacter
75 "2+2=4" =~ /2\+2/; # matches, \+ is treated like an ordinary +
76 'C:\WIN32' =~ /C:\\WIN/; # matches
77 "/usr/bin/perl" =~ /\/usr\/local\/bin\/perl/; # matches
78
6425a278 79In the last regex, the forward slash C<'/'> is also backslashed,
80because it is used to delimit the regex.
47f9c88b 81
82Non-printable ASCII characters are represented by B<escape sequences>.
83Common examples are C<\t> for a tab, C<\n> for a newline, and C<\r>
84for a carriage return. Arbitrary bytes are represented by octal
85escape sequences, e.g., C<\033>, or hexadecimal escape sequences,
86e.g., C<\x1B>:
87
88 "1000\t2000" =~ m(0\t2) # matches
89 "cat" =~ /\143\x61\x74/ # matches, but a weird way to spell cat
90
6425a278 91Regexes are treated mostly as double quoted strings, so variable
47f9c88b 92substitution works:
93
94 $foo = 'house';
95 'cathouse' =~ /cat$foo/; # matches
96 'housecat' =~ /${foo}cat/; # matches
97
6425a278 98With all of the regexes above, if the regex matched anywhere in the
47f9c88b 99string, it was considered a match. To specify I<where> it should
100match, we would use the B<anchor> metacharacters C<^> and C<$>. The
101anchor C<^> means match at the beginning of the string and the anchor
102C<$> means match at the end of the string, or before a newline at the
103end of the string. Some examples:
104
6425a278 105 "housekeeper" =~ /keeper/; # matches
106 "housekeeper" =~ /^keeper/; # doesn't match
107 "housekeeper" =~ /keeper$/; # matches
108 "housekeeper\n" =~ /keeper$/; # matches
109 "housekeeper" =~ /^housekeeper$/; # matches
47f9c88b 110
111=head2 Using character classes
112
113A B<character class> allows a set of possible characters, rather than
6425a278 114just a single character, to match at a particular point in a regex.
47f9c88b 115Character classes are denoted by brackets C<[...]>, with the set of
116characters to be possibly matched inside. Here are some examples:
117
118 /cat/; # matches 'cat'
6425a278 119 /[bcr]at/; # matches 'bat', 'cat', or 'rat'
47f9c88b 120 "abc" =~ /[cab]/; # matches 'a'
121
122In the last statement, even though C<'c'> is the first character in
6425a278 123the class, the earliest point at which the regex can match is C<'a'>.
47f9c88b 124
125 /[yY][eE][sS]/; # match 'yes' in a case-insensitive way
126 # 'yes', 'Yes', 'YES', etc.
127 /yes/i; # also match 'yes' in a case-insensitive way
128
129The last example shows a match with an C<'i'> B<modifier>, which makes
130the match case-insensitive.
131
132Character classes also have ordinary and special characters, but the
133sets of ordinary and special characters inside a character class are
134different than those outside a character class. The special
135characters for a character class are C<-]\^$> and are matched using an
136escape:
137
138 /[\]c]def/; # matches ']def' or 'cdef'
139 $x = 'bcr';
140 /[$x]at/; # matches 'bat, 'cat', or 'rat'
141 /[\$x]at/; # matches '$at' or 'xat'
142 /[\\$x]at/; # matches '\at', 'bat, 'cat', or 'rat'
143
144The special character C<'-'> acts as a range operator within character
145classes, so that the unwieldy C<[0123456789]> and C<[abc...xyz]>
146become the svelte C<[0-9]> and C<[a-z]>:
147
148 /item[0-9]/; # matches 'item0' or ... or 'item9'
149 /[0-9a-fA-F]/; # matches a hexadecimal digit
150
151If C<'-'> is the first or last character in a character class, it is
152treated as an ordinary character.
153
154The special character C<^> in the first position of a character class
155denotes a B<negated character class>, which matches any character but
6425a278 156those in the brackets. Both C<[...]> and C<[^...]> must match a
47f9c88b 157character, or the match fails. Then
158
159 /[^a]at/; # doesn't match 'aat' or 'at', but matches
160 # all other 'bat', 'cat, '0at', '%at', etc.
161 /[^0-9]/; # matches a non-numeric character
162 /[a^]at/; # matches 'aat' or '^at'; here '^' is ordinary
163
164Perl has several abbreviations for common character classes:
165
166=over 4
167
168=item *
551e1d92 169
47f9c88b 170\d is a digit and represents [0-9]
171
172=item *
551e1d92 173
47f9c88b 174\s is a whitespace character and represents [\ \t\r\n\f]
175
176=item *
551e1d92 177
47f9c88b 178\w is a word character (alphanumeric or _) and represents [0-9a-zA-Z_]
179
180=item *
551e1d92 181
47f9c88b 182\D is a negated \d; it represents any character but a digit [^0-9]
183
184=item *
551e1d92 185
47f9c88b 186\S is a negated \s; it represents any non-whitespace character [^\s]
187
188=item *
551e1d92 189
47f9c88b 190\W is a negated \w; it represents any non-word character [^\w]
191
192=item *
551e1d92 193
47f9c88b 194The period '.' matches any character but "\n"
195
196=back
197
198The C<\d\s\w\D\S\W> abbreviations can be used both inside and outside
199of character classes. Here are some in use:
200
201 /\d\d:\d\d:\d\d/; # matches a hh:mm:ss time format
202 /[\d\s]/; # matches any digit or whitespace character
203 /\w\W\w/; # matches a word char, followed by a
204 # non-word char, followed by a word char
205 /..rt/; # matches any two chars, followed by 'rt'
206 /end\./; # matches 'end.'
207 /end[.]/; # same thing, matches 'end.'
208
209The S<B<word anchor> > C<\b> matches a boundary between a word
210character and a non-word character C<\w\W> or C<\W\w>:
211
212 $x = "Housecat catenates house and cat";
213 $x =~ /\bcat/; # matches cat in 'catenates'
214 $x =~ /cat\b/; # matches cat in 'housecat'
215 $x =~ /\bcat\b/; # matches 'cat' at end of string
216
217In the last example, the end of the string is considered a word
218boundary.
219
220=head2 Matching this or that
221
222We can match match different character strings with the B<alternation>
6425a278 223metacharacter C<'|'>. To match C<dog> or C<cat>, we form the regex
224C<dog|cat>. As before, perl will try to match the regex at the
47f9c88b 225earliest possible point in the string. At each character position,
226perl will first try to match the the first alternative, C<dog>. If
227C<dog> doesn't match, perl will then try the next alternative, C<cat>.
228If C<cat> doesn't match either, then the match fails and perl moves to
229the next position in the string. Some examples:
230
231 "cats and dogs" =~ /cat|dog|bird/; # matches "cat"
232 "cats and dogs" =~ /dog|cat|bird/; # matches "cat"
233
6425a278 234Even though C<dog> is the first alternative in the second regex,
47f9c88b 235C<cat> is able to match earlier in the string.
236
237 "cats" =~ /c|ca|cat|cats/; # matches "c"
238 "cats" =~ /cats|cat|ca|c/; # matches "cats"
239
240At a given character position, the first alternative that allows the
6425a278 241regex match to succeed wil be the one that matches. Here, all the
47f9c88b 242alternatives match at the first string position, so th first matches.
243
244=head2 Grouping things and hierarchical matching
245
6425a278 246The B<grouping> metacharacters C<()> allow a part of a regex to be
247treated as a single unit. Parts of a regex are grouped by enclosing
248them in parentheses. The regex C<house(cat|keeper)> means match
47f9c88b 249C<house> followed by either C<cat> or C<keeper>. Some more examples
250are
251
252 /(a|b)b/; # matches 'ab' or 'bb'
253 /(^a|b)c/; # matches 'ac' at start of string or 'bc' anywhere
254
255 /house(cat|)/; # matches either 'housecat' or 'house'
256 /house(cat(s|)|)/; # matches either 'housecats' or 'housecat' or
257 # 'house'. Note groups can be nested.
258
259 "20" =~ /(19|20|)\d\d/; # matches the null alternative '()\d\d',
260 # because '20\d\d' can't match
261
262=head2 Extracting matches
263
264The grouping metacharacters C<()> also allow the extraction of the
265parts of a string that matched. For each grouping, the part that
266matched inside goes into the special variables C<$1>, C<$2>, etc.
267They can be used just as ordinary variables:
268
269 # extract hours, minutes, seconds
270 $time =~ /(\d\d):(\d\d):(\d\d)/; # match hh:mm:ss format
271 $hours = $1;
272 $minutes = $2;
273 $seconds = $3;
274
6425a278 275In list context, a match C</regex/> with groupings will return the
47f9c88b 276list of matched values C<($1,$2,...)>. So we could rewrite it as
277
278 ($hours, $minutes, $second) = ($time =~ /(\d\d):(\d\d):(\d\d)/);
279
6425a278 280If the groupings in a regex are nested, C<$1> gets the group with the
47f9c88b 281leftmost opening parenthesis, C<$2> the next opening parenthesis,
6425a278 282etc. For example, here is a complex regex and the matching variables
47f9c88b 283indicated below it:
284
285 /(ab(cd|ef)((gi)|j))/;
286 1 2 34
287
288Associated with the matching variables C<$1>, C<$2>, ... are
289the B<backreferences> C<\1>, C<\2>, ... Backreferences are
6425a278 290matching variables that can be used I<inside> a regex:
47f9c88b 291
292 /(\w\w\w)\s\1/; # find sequences like 'the the' in string
293
6425a278 294C<$1>, C<$2>, ... should only be used outside of a regex, and C<\1>,
295C<\2>, ... only inside a regex.
47f9c88b 296
297=head2 Matching repetitions
298
299The B<quantifier> metacharacters C<?>, C<*>, C<+>, and C<{}> allow us
6425a278 300to determine the number of repeats of a portion of a regex we
47f9c88b 301consider to be a match. Quantifiers are put immediately after the
302character, character class, or grouping that we want to specify. They
303have the following meanings:
304
305=over 4
306
307=item * C<a?> = match 'a' 1 or 0 times
308
309=item * C<a*> = match 'a' 0 or more times, i.e., any number of times
310
311=item * C<a+> = match 'a' 1 or more times, i.e., at least once
312
313=item * C<a{n,m}> = match at least C<n> times, but not more than C<m>
314times.
315
316=item * C<a{n,}> = match at least C<n> or more times
317
318=item * C<a{n}> = match exactly C<n> times
319
320=back
321
322Here are some examples:
323
324 /[a-z]+\s+\d*/; # match a lowercase word, at least some space, and
325 # any number of digits
326 /(\w+)\s+\1/; # match doubled words of arbitrary length
327 $year =~ /\d{2,4}/; # make sure year is at least 2 but not more
328 # than 4 digits
329 $year =~ /\d{4}|\d{2}/; # better match; throw out 3 digit dates
330
331These quantifiers will try to match as much of the string as possible,
6425a278 332while still allowing the regex to match. So we have
47f9c88b 333
6425a278 334 $x = 'the cat in the hat';
47f9c88b 335 $x =~ /^(.*)(at)(.*)$/; # matches,
336 # $1 = 'the cat in the h'
337 # $2 = 'at'
338 # $3 = '' (0 matches)
339
340The first quantifier C<.*> grabs as much of the string as possible
6425a278 341while still having the regex match. The second quantifier C<.*> has
47f9c88b 342no string left to it, so it matches 0 times.
343
344=head2 More matching
345
346There are a few more things you might want to know about matching
347operators. In the code
348
349 $pattern = 'Seuss';
350 while (<>) {
351 print if /$pattern/;
352 }
353
354perl has to re-evaluate C<$pattern> each time through the loop. If
355C<$pattern> won't be changing, use the C<//o> modifier, to only
356perform variable substitutions once. If you don't want any
357substitutions at all, use the special delimiter C<m''>:
358
359 $pattern = 'Seuss';
360 m'$pattern'; # matches '$pattern', not 'Seuss'
361
362The global modifier C<//g> allows the matching operator to match
363within a string as many times as possible. In scalar context,
364successive matches against a string will have C<//g> jump from match
365to match, keeping track of position in the string as it goes along.
366You can get or set the position with the C<pos()> function.
367For example,
368
369 $x = "cat dog house"; # 3 words
370 while ($x =~ /(\w+)/g) {
371 print "Word is $1, ends at position ", pos $x, "\n";
372 }
373
374prints
375
376 Word is cat, ends at position 3
377 Word is dog, ends at position 7
378 Word is house, ends at position 13
379
380A failed match or changing the target string resets the position. If
381you don't want the position reset after failure to match, add the
6425a278 382C<//c>, as in C</regex/gc>.
47f9c88b 383
384In list context, C<//g> returns a list of matched groupings, or if
6425a278 385there are no groupings, a list of matches to the whole regex. So
47f9c88b 386
387 @words = ($x =~ /(\w+)/g); # matches,
388 # $word[0] = 'cat'
389 # $word[1] = 'dog'
390 # $word[2] = 'house'
391
392=head2 Search and replace
393
6425a278 394Search and replace is performed using C<s/regex/replacement/modifiers>.
47f9c88b 395The C<replacement> is a Perl double quoted string that replaces in the
6425a278 396string whatever is matched with the C<regex>. The operator C<=~> is
47f9c88b 397also used here to associate a string with C<s///>. If matching
398against C<$_>, the S<C<$_ =~> > can be dropped. If there is a match,
399C<s///> returns the number of substitutions made, otherwise it returns
400false. Here are a few examples:
401
402 $x = "Time to feed the cat!";
403 $x =~ s/cat/hacker/; # $x contains "Time to feed the hacker!"
404 $y = "'quoted words'";
405 $y =~ s/^'(.*)'$/$1/; # strip single quotes,
406 # $y contains "quoted words"
407
408With the C<s///> operator, the matched variables C<$1>, C<$2>, etc.
409are immediately available for use in the replacement expression. With
410the global modifier, C<s///g> will search and replace all occurrences
6425a278 411of the regex in the string:
47f9c88b 412
413 $x = "I batted 4 for 4";
414 $x =~ s/4/four/; # $x contains "I batted four for 4"
415 $x = "I batted 4 for 4";
416 $x =~ s/4/four/g; # $x contains "I batted four for four"
417
418The evaluation modifier C<s///e> wraps an C<eval{...}> around the
419replacement string and the evaluated result is substituted for the
6425a278 420matched substring. Some examples:
47f9c88b 421
6425a278 422 # reverse all the words in a string
423 $x = "the cat in the hat";
424 $x =~ s/(\w+)/reverse $1/ge; # $x contains "eht tac ni eht tah"
47f9c88b 425
6425a278 426 # convert percentage to decimal
427 $x = "A 39% hit rate";
428 $x =~ s!(\d+)%!$1/100!e; # $x contains "A 0.39 hit rate"
47f9c88b 429
6425a278 430The last example shows that C<s///> can use other delimiters, such as
431C<s!!!> and C<s{}{}>, and even C<s{}//>. If single quotes are used
432C<s'''>, then the regex and replacement are treated as single quoted
433strings.
47f9c88b 434
435=head2 The split operator
436
6425a278 437C<split /regex/, string> splits C<string> into a list of substrings
438and returns that list. The regex determines the character sequence
47f9c88b 439that C<string> is split with respect to. For example, to split a
440string into words, use
441
442 $x = "Calvin and Hobbes";
6425a278 443 @word = split /\s+/, $x; # $word[0] = 'Calvin'
444 # $word[1] = 'and'
445 # $word[2] = 'Hobbes'
446
447To extract a comma-delimited list of numbers, use
47f9c88b 448
6425a278 449 $x = "1.618,2.718, 3.142";
450 @const = split /,\s*/, $x; # $const[0] = '1.618'
451 # $const[1] = '2.718'
452 # $const[2] = '3.142'
453
454If the empty regex C<//> is used, the string is split into individual
455characters. If the regex has groupings, then list produced contains
47f9c88b 456the matched substrings from the groupings as well:
457
458 $x = "/usr/bin";
459 @parts = split m!(/)!, $x; # $parts[0] = ''
460 # $parts[1] = '/'
461 # $parts[2] = 'usr'
462 # $parts[3] = '/'
463 # $parts[4] = 'bin'
464
6425a278 465Since the first character of $x matched the regex, C<split> prepended
47f9c88b 466an empty initial element to the list.
467
468=head1 BUGS
469
470None.
471
472=head1 SEE ALSO
473
474This is just a quick start guide. For a more in-depth tutorial on
6425a278 475regexes, see L<perlretut> and for the reference page, see L<perlre>.
47f9c88b 476
477=head1 AUTHOR AND COPYRIGHT
478
479Copyright (c) 2000 Mark Kvale
480All rights reserved.
481
482This document may be distributed under the same terms as Perl itself.
483
6425a278 484=head2 Acknowledgments
485
486The author would like to thank Mark-Jason Dominus, Tom Christiansen,
487Ilya Zakharevich, Brad Hughes, and Mike Giroux for all their helpful
488comments.
489
47f9c88b 490=cut
491