Debian Codegraph @

upstream/fedora-44/man1/perlrequick.1

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "PERLREQUICK 1"
61 .TH PERLREQUICK 1 2026-03-09 "perl v5.42.1" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 perlrequick \- Perl regular expressions quick start
68 .SH DESCRIPTION
69 .IX Header "DESCRIPTION"
70 This page covers the very basics of understanding, creating and
71 using regular expressions (\*(Aqregexes\*(Aq) in Perl.
72 .SH "The Guide"
73 .IX Header "The Guide"
74 This page assumes you already know things, like what a "pattern" is, and
75 the basic syntax of using them. If you don\*(Aqt, see perlretut.
76 .SS "Simple word matching"
77 .IX Subsection "Simple word matching"
78 The simplest regex is simply a word, or more generally, a string of
79 characters. A regex consisting of a word matches any string that
80 contains that word:
81 .PP
82 .Vb 1
83 \& "Hello World" =~ /World/; # matches
84 .Ve
85 .PP
86 In this statement, \f(CW\*(C`World\*(C'\fR is a regex and the \f(CW\*(C`//\*(C'\fR enclosing
87 \&\f(CW\*(C`/World/\*(C'\fR tells Perl to search a string for a match. The operator
88 \&\f(CW\*(C`=~\*(C'\fR associates the string with the regex match and produces a true
89 value if the regex matched, or false if the regex did not match. In
90 our case, \f(CW\*(C`World\*(C'\fR matches the second word in \f(CW"Hello World"\fR, so the
91 expression is true. This idea has several variations.
92 .PP
93 Expressions like this are useful in conditionals:
94 .PP
95 .Vb 1
96 \& print "It matches\en" if "Hello World" =~ /World/;
97 .Ve
98 .PP
99 The sense of the match can be reversed by using \f(CW\*(C`!~\*(C'\fR operator:
100 .PP
101 .Vb 1
102 \& print "It doesn\*(Aqt match\en" if "Hello World" !~ /World/;
103 .Ve
104 .PP
105 The literal string in the regex can be replaced by a variable:
106 .PP
107 .Vb 2
108 \& $greeting = "World";
109 \& print "It matches\en" if "Hello World" =~ /$greeting/;
110 .Ve
111 .PP
112 If you\*(Aqre matching against \f(CW$_\fR, the \f(CW\*(C`$_ =~\*(C'\fR part can be omitted:
113 .PP
114 .Vb 2
115 \& $_ = "Hello World";
116 \& print "It matches\en" if /World/;
117 .Ve
118 .PP
119 Finally, the \f(CW\*(C`//\*(C'\fR default delimiters for a match can be changed to
120 arbitrary delimiters by putting an \f(CW\*(Aqm\*(Aq\fR out front:
121 .PP
122 .Vb 4
123 \& "Hello World" =~ m!World!; # matches, delimited by \*(Aq!\*(Aq
124 \& "Hello World" =~ m{World}; # matches, note the matching \*(Aq{}\*(Aq
125 \& "/usr/bin/perl" =~ m"/perl"; # matches after \*(Aq/usr/bin\*(Aq,
126 \& # \*(Aq/\*(Aq becomes an ordinary char
127 .Ve
128 .PP
129 Regexes must match a part of the string \fIexactly\fR in order for the
130 statement to be true:
131 .PP
132 .Vb 3
133 \& "Hello World" =~ /world/; # doesn\*(Aqt match, case sensitive
134 \& "Hello World" =~ /o W/; # matches, \*(Aq \*(Aq is an ordinary char
135 \& "Hello World" =~ /World /; # doesn\*(Aqt match, no \*(Aq \*(Aq at end
136 .Ve
137 .PP
138 Perl will always match at the earliest possible point in the string:
139 .PP
140 .Vb 2
141 \& "Hello World" =~ /o/; # matches \*(Aqo\*(Aq in \*(AqHello\*(Aq
142 \& "That hat is red" =~ /hat/; # matches \*(Aqhat\*(Aq in \*(AqThat\*(Aq
143 .Ve
144 .PP
145 Not all characters can be used \*(Aqas is\*(Aq in a match. Some characters,
146 called \fBmetacharacters\fR, are considered special, and reserved for use
147 in regex notation. The metacharacters are
148 .PP
149 .Vb 1
150 \& {}[]()^$.|*+?\e
151 .Ve
152 .PP
153 A metacharacter can be matched literally by putting a backslash before
154 it:
155 .PP
156 .Vb 4
157 \& "2+2=4" =~ /2+2/; # doesn\*(Aqt match, + is a metacharacter
158 \& "2+2=4" =~ /2\e+2/; # matches, \e+ is treated like an ordinary +
159 \& \*(AqC:\eWIN32\*(Aq =~ /C:\e\eWIN/; # matches
160 \& "/usr/bin/perl" =~ /\e/usr\e/bin\e/perl/; # matches
161 .Ve
162 .PP
163 In the last regex, the forward slash \f(CW\*(Aq/\*(Aq\fR is also backslashed,
164 because it is used to delimit the regex.
165 .PP
166 Most of the metacharacters aren\*(Aqt always special, and other characters
167 (such as the ones delimiting the pattern) become special under various
168 circumstances. This can be confusing and lead to unexpected results.
169 \&\f(CW\*(C`use\ re\ \*(Aqstrict\*(Aq\*(C'\fR can notify you of potential
170 pitfalls.
171 .PP
172 Non\-printable ASCII characters are represented by \fBescape sequences\fR.
173 Common examples are \f(CW\*(C`\et\*(C'\fR for a tab, \f(CW\*(C`\en\*(C'\fR for a newline, and \f(CW\*(C`\er\*(C'\fR
174 for a carriage return. Arbitrary bytes are represented by octal
175 escape sequences, e.g., \f(CW\*(C`\e033\*(C'\fR, or hexadecimal escape sequences,
176 e.g., \f(CW\*(C`\ex1B\*(C'\fR:
177 .PP
178 .Vb 3
179 \& "1000\et2000" =~ m(0\et2) # matches
180 \& "cat" =~ /\e143\ex61\ex74/ # matches in ASCII, but
181 \& # a weird way to spell cat
182 .Ve
183 .PP
184 Regexes are treated mostly as double\-quoted strings, so variable
185 substitution works:
186 .PP
187 .Vb 3
188 \& $foo = \*(Aqhouse\*(Aq;
189 \& \*(Aqcathouse\*(Aq =~ /cat$foo/; # matches
190 \& \*(Aqhousecat\*(Aq =~ /${foo}cat/; # matches
191 .Ve
192 .PP
193 With all of the regexes above, if the regex matched anywhere in the
194 string, it was considered a match. To specify \fIwhere\fR it should
195 match, we would use the \fBanchor\fR metacharacters \f(CW\*(C`^\*(C'\fR and \f(CW\*(C`$\*(C'\fR. The
196 anchor \f(CW\*(C`^\*(C'\fR means match at the beginning of the string and the anchor
197 \&\f(CW\*(C`$\*(C'\fR means match at the end of the string, or before a newline at the
198 end of the string. Some examples:
199 .PP
200 .Vb 5
201 \& "housekeeper" =~ /keeper/; # matches
202 \& "housekeeper" =~ /^keeper/; # doesn\*(Aqt match
203 \& "housekeeper" =~ /keeper$/; # matches
204 \& "housekeeper\en" =~ /keeper$/; # matches
205 \& "housekeeper" =~ /^housekeeper$/; # matches
206 .Ve
207 .SS "Using character classes"
208 .IX Subsection "Using character classes"
209 A \fBcharacter class\fR allows a set of possible characters, rather than
210 just a single character, to match at a particular point in a regex.
211 There are a number of different types of character classes, but usually
212 when people use this term, they are referring to the type described in
213 this section, which are technically called "Bracketed character
214 classes", because they are denoted by brackets \f(CW\*(C`[...]\*(C'\fR, with the set of
215 characters to be possibly matched inside. But we\*(Aqll drop the "bracketed"
216 below to correspond with common usage. Here are some examples of
217 (bracketed) character classes:
218 .PP
219 .Vb 3
220 \& /cat/; # matches \*(Aqcat\*(Aq
221 \& /[bcr]at/; # matches \*(Aqbat\*(Aq, \*(Aqcat\*(Aq, or \*(Aqrat\*(Aq
222 \& "abc" =~ /[cab]/; # matches \*(Aqa\*(Aq
223 .Ve
224 .PP
225 In the last statement, even though \f(CW\*(Aqc\*(Aq\fR is the first character in
226 the class, the earliest point at which the regex can match is \f(CW\*(Aqa\*(Aq\fR.
227 .PP
228 .Vb 3
229 \& /[yY][eE][sS]/; # match \*(Aqyes\*(Aq in a case\-insensitive way
230 \& # \*(Aqyes\*(Aq, \*(AqYes\*(Aq, \*(AqYES\*(Aq, etc.
231 \& /yes/i; # also match \*(Aqyes\*(Aq in a case\-insensitive way
232 .Ve
233 .PP
234 The last example shows a match with an \f(CW\*(Aqi\*(Aq\fR \fBmodifier\fR, which makes
235 the match case\-insensitive.
236 .PP
237 Character classes also have ordinary and special characters, but the
238 sets of ordinary and special characters inside a character class are
239 different than those outside a character class. The special
240 characters for a character class are \f(CW\*(C`\-]\e^$\*(C'\fR and are matched using an
241 escape:
242 .PP
243 .Vb 5
244 \& /[\e]c]def/; # matches \*(Aq]def\*(Aq or \*(Aqcdef\*(Aq
245 \& $x = \*(Aqbcr\*(Aq;
246 \& /[$x]at/; # matches \*(Aqbat, \*(Aqcat\*(Aq, or \*(Aqrat\*(Aq
247 \& /[\e$x]at/; # matches \*(Aq$at\*(Aq or \*(Aqxat\*(Aq
248 \& /[\e\e$x]at/; # matches \*(Aq\eat\*(Aq, \*(Aqbat, \*(Aqcat\*(Aq, or \*(Aqrat\*(Aq
249 .Ve
250 .PP
251 The special character \f(CW\*(Aq\-\*(Aq\fR acts as a range operator within character
252 classes, so that the unwieldy \f(CW\*(C`[0123456789]\*(C'\fR and \f(CW\*(C`[abc...xyz]\*(C'\fR
253 become the svelte \f(CW\*(C`[0\-9]\*(C'\fR and \f(CW\*(C`[a\-z]\*(C'\fR:
254 .PP
255 .Vb 2
256 \& /item[0\-9]/; # matches \*(Aqitem0\*(Aq or ... or \*(Aqitem9\*(Aq
257 \& /[0\-9a\-fA\-F]/; # matches a hexadecimal digit
258 .Ve
259 .PP
260 If \f(CW\*(Aq\-\*(Aq\fR is the first or last character in a character class, it is
261 treated as an ordinary character.
262 .PP
263 The special character \f(CW\*(C`^\*(C'\fR in the first position of a character class
264 denotes a \fBnegated character class\fR, which matches any character but
265 those in the brackets. Both \f(CW\*(C`[...]\*(C'\fR and \f(CW\*(C`[^...]\*(C'\fR must match a
266 character, or the match fails. Then
267 .PP
268 .Vb 4
269 \& /[^a]at/; # doesn\*(Aqt match \*(Aqaat\*(Aq or \*(Aqat\*(Aq, but matches
270 \& # all other \*(Aqbat\*(Aq, \*(Aqcat, \*(Aq0at\*(Aq, \*(Aq%at\*(Aq, etc.
271 \& /[^0\-9]/; # matches a non\-numeric character
272 \& /[a^]at/; # matches \*(Aqaat\*(Aq or \*(Aq^at\*(Aq; here \*(Aq^\*(Aq is ordinary
273 .Ve
274 .PP
275 Perl has several abbreviations for common character classes. (These
276 definitions are those that Perl uses in ASCII\-safe mode with the \f(CW\*(C`/a\*(C'\fR modifier.
277 Otherwise they could match many more non\-ASCII Unicode characters as
278 well. See "Backslash sequences" in perlrecharclass for details.)
279 .IP \(bu 4
280 \&\ed is a digit and represents
281 .Sp
282 .Vb 1
283 \& [0\-9]
284 .Ve
285 .IP \(bu 4
286 \&\es is a whitespace character and represents
287 .Sp
288 .Vb 1
289 \& [\e \et\er\en\ef]
290 .Ve
291 .IP \(bu 4
292 \&\ew is a word character (alphanumeric or _) and represents
293 .Sp
294 .Vb 1
295 \& [0\-9a\-zA\-Z_]
296 .Ve
297 .IP \(bu 4
298 \&\eD is a negated \ed; it represents any character but a digit
299 .Sp
300 .Vb 1
301 \& [^0\-9]
302 .Ve
303 .IP \(bu 4
304 \&\eS is a negated \es; it represents any non\-whitespace character
305 .Sp
306 .Vb 1
307 \& [^\es]
308 .Ve
309 .IP \(bu 4
310 \&\eW is a negated \ew; it represents any non\-word character
311 .Sp
312 .Vb 1
313 \& [^\ew]
314 .Ve
315 .IP \(bu 4
316 The period \*(Aq.\*(Aq matches any character but "\en"
317 .PP
318 The \f(CW\*(C`\ed\es\ew\eD\eS\eW\*(C'\fR abbreviations can be used both inside and outside
319 of character classes. Here are some in use:
320 .PP
321 .Vb 7
322 \& /\ed\ed:\ed\ed:\ed\ed/; # matches a hh:mm:ss time format
323 \& /[\ed\es]/; # matches any digit or whitespace character
324 \& /\ew\eW\ew/; # matches a word char, followed by a
325 \& # non\-word char, followed by a word char
326 \& /..rt/; # matches any two chars, followed by \*(Aqrt\*(Aq
327 \& /end\e./; # matches \*(Aqend.\*(Aq
328 \& /end[.]/; # same thing, matches \*(Aqend.\*(Aq
329 .Ve
330 .PP
331 The \fBword\ anchor\fR\ \f(CW\*(C`\eb\*(C'\fR matches a boundary between a word
332 character and a non\-word character \f(CW\*(C`\ew\eW\*(C'\fR or \f(CW\*(C`\eW\ew\*(C'\fR:
333 .PP
334 .Vb 4
335 \& $x = "Housecat catenates house and cat";
336 \& $x =~ /\ebcat/; # matches cat in \*(Aqcatenates\*(Aq
337 \& $x =~ /cat\eb/; # matches cat in \*(Aqhousecat\*(Aq
338 \& $x =~ /\ebcat\eb/; # matches \*(Aqcat\*(Aq at end of string
339 .Ve
340 .PP
341 In the last example, the end of the string is considered a word
342 boundary.
343 .PP
344 For natural language processing (so that, for example, apostrophes are
345 included in words), use instead \f(CW\*(C`\eb{wb}\*(C'\fR
346 .PP
347 .Vb 1
348 \& "don\*(Aqt" =~ / .+? \eb{wb} /x; # matches the whole string
349 .Ve
350 .SS "Matching this or that"
351 .IX Subsection "Matching this or that"
352 We can match different character strings with the \fBalternation\fR
353 metacharacter \f(CW\*(Aq|\*(Aq\fR. To match \f(CW\*(C`dog\*(C'\fR or \f(CW\*(C`cat\*(C'\fR, we form the regex
354 \&\f(CW\*(C`dog|cat\*(C'\fR. As before, Perl will try to match the regex at the
355 earliest possible point in the string. At each character position,
356 Perl will first try to match the first alternative, \f(CW\*(C`dog\*(C'\fR. If
357 \&\f(CW\*(C`dog\*(C'\fR doesn\*(Aqt match, Perl will then try the next alternative, \f(CW\*(C`cat\*(C'\fR.
358 If \f(CW\*(C`cat\*(C'\fR doesn\*(Aqt match either, then the match fails and Perl moves to
359 the next position in the string. Some examples:
360 .PP
361 .Vb 2
362 \& "cats and dogs" =~ /cat|dog|bird/; # matches "cat"
363 \& "cats and dogs" =~ /dog|cat|bird/; # matches "cat"
364 .Ve
365 .PP
366 Even though \f(CW\*(C`dog\*(C'\fR is the first alternative in the second regex,
367 \&\f(CW\*(C`cat\*(C'\fR is able to match earlier in the string.
368 .PP
369 .Vb 2
370 \& "cats" =~ /c|ca|cat|cats/; # matches "c"
371 \& "cats" =~ /cats|cat|ca|c/; # matches "cats"
372 .Ve
373 .PP
374 At a given character position, the first alternative that allows the
375 regex match to succeed will be the one that matches. Here, all the
376 alternatives match at the first string position, so the first matches.
377 .SS "Grouping things and hierarchical matching"
378 .IX Subsection "Grouping things and hierarchical matching"
379 The \fBgrouping\fR metacharacters \f(CW\*(C`()\*(C'\fR allow a part of a regex to be
380 treated as a single unit. Parts of a regex are grouped by enclosing
381 them in parentheses. The regex \f(CWhouse(cat|keeper)\fR means match
382 \&\f(CW\*(C`house\*(C'\fR followed by either \f(CW\*(C`cat\*(C'\fR or \f(CW\*(C`keeper\*(C'\fR. Some more examples
383 are
384 .PP
385 .Vb 2
386 \& /(a|b)b/; # matches \*(Aqab\*(Aq or \*(Aqbb\*(Aq
387 \& /(^a|b)c/; # matches \*(Aqac\*(Aq at start of string or \*(Aqbc\*(Aq anywhere
388 \&
389 \& /house(cat|)/; # matches either \*(Aqhousecat\*(Aq or \*(Aqhouse\*(Aq
390 \& /house(cat(s|)|)/; # matches either \*(Aqhousecats\*(Aq or \*(Aqhousecat\*(Aq or
391 \& # \*(Aqhouse\*(Aq. Note groups can be nested.
392 \&
393 \& "20" =~ /(19|20|)\ed\ed/; # matches the null alternative \*(Aq()\ed\ed\*(Aq,
394 \& # because \*(Aq20\ed\ed\*(Aq can\*(Aqt match
395 .Ve
396 .SS "Extracting matches"
397 .IX Subsection "Extracting matches"
398 The grouping metacharacters \f(CW\*(C`()\*(C'\fR also allow the extraction of the
399 parts of a string that matched. For each grouping, the part that
400 matched inside goes into the special variables \f(CW$1\fR, \f(CW$2\fR, etc.
401 They can be used just as ordinary variables:
402 .PP
403 .Vb 5
404 \& # extract hours, minutes, seconds
405 \& $time =~ /(\ed\ed):(\ed\ed):(\ed\ed)/; # match hh:mm:ss format
406 \& $hours = $1;
407 \& $minutes = $2;
408 \& $seconds = $3;
409 .Ve
410 .PP
411 In list context, a match \f(CW\*(C`/regex/\*(C'\fR with groupings will return the
412 list of matched values \f(CW\*(C`($1,$2,...)\*(C'\fR. So we could rewrite it as
413 .PP
414 .Vb 1
415 \& ($hours, $minutes, $second) = ($time =~ /(\ed\ed):(\ed\ed):(\ed\ed)/);
416 .Ve
417 .PP
418 If the groupings in a regex are nested, \f(CW$1\fR gets the group with the
419 leftmost opening parenthesis, \f(CW$2\fR the next opening parenthesis,
420 etc. For example, here is a complex regex and the matching variables
421 indicated below it:
422 .PP
423 .Vb 2
424 \& /(ab(cd|ef)((gi)|j))/;
425 \& 1 2 34
426 .Ve
427 .PP
428 Associated with the matching variables \f(CW$1\fR, \f(CW$2\fR, ... are
429 the \fBbackreferences\fR \f(CW\*(C`\eg1\*(C'\fR, \f(CW\*(C`\eg2\*(C'\fR, ... Backreferences are
430 matching variables that can be used \fIinside\fR a regex:
431 .PP
432 .Vb 1
433 \& /(\ew\ew\ew)\es\eg1/; # find sequences like \*(Aqthe the\*(Aq in string
434 .Ve
435 .PP
436 \&\f(CW$1\fR, \f(CW$2\fR, ... should only be used outside of a regex, and \f(CW\*(C`\eg1\*(C'\fR,
437 \&\f(CW\*(C`\eg2\*(C'\fR, ... only inside a regex.
438 .SS "Matching repetitions"
439 .IX Subsection "Matching repetitions"
440 The \fBquantifier\fR metacharacters \f(CW\*(C`?\*(C'\fR, \f(CW\*(C`*\*(C'\fR, \f(CW\*(C`+\*(C'\fR, and \f(CW\*(C`{}\*(C'\fR allow us
441 to determine the number of repeats of a portion of a regex we
442 consider to be a match. Quantifiers are put immediately after the
443 character, character class, or grouping that we want to specify. They
444 have the following meanings:
445 .IP \(bu 4
446 \&\f(CW\*(C`a?\*(C'\fR = match \*(Aqa\*(Aq 1 or 0 times
447 .IP \(bu 4
448 \&\f(CW\*(C`a*\*(C'\fR = match \*(Aqa\*(Aq 0 or more times, i.e., any number of times
449 .IP \(bu 4
450 \&\f(CW\*(C`a+\*(C'\fR = match \*(Aqa\*(Aq 1 or more times, i.e., at least once
451 .IP \(bu 4
452 \&\f(CW\*(C`a{n,m}\*(C'\fR = match at least \f(CW\*(C`n\*(C'\fR times, but not more than \f(CW\*(C`m\*(C'\fR
453 times.
454 .IP \(bu 4
455 \&\f(CW\*(C`a{n,}\*(C'\fR = match at least \f(CW\*(C`n\*(C'\fR or more times
456 .IP \(bu 4
457 \&\f(CW\*(C`a{,n}\*(C'\fR = match \f(CW\*(C`n\*(C'\fR times or fewer (Added in v5.34)
458 .IP \(bu 4
459 \&\f(CW\*(C`a{n}\*(C'\fR = match exactly \f(CW\*(C`n\*(C'\fR times
460 .PP
461 Here are some examples:
462 .PP
463 .Vb 6
464 \& /[a\-z]+\es+\ed*/; # match a lowercase word, at least some space, and
465 \& # any number of digits
466 \& /(\ew+)\es+\eg1/; # match doubled words of arbitrary length
467 \& $year =~ /^\ed{2,4}$/; # make sure year is at least 2 but not more
468 \& # than 4 digits
469 \& $year =~ /^\ed{ 4 }$|^\ed{2}$/; # better match; throw out 3 digit dates
470 .Ve
471 .PP
472 These quantifiers will try to match as much of the string as possible,
473 while still allowing the regex to match. So we have
474 .PP
475 .Vb 5
476 \& $x = \*(Aqthe cat in the hat\*(Aq;
477 \& $x =~ /^(.*)(at)(.*)$/; # matches,
478 \& # $1 = \*(Aqthe cat in the h\*(Aq
479 \& # $2 = \*(Aqat\*(Aq
480 \& # $3 = \*(Aq\*(Aq (0 matches)
481 .Ve
482 .PP
483 The first quantifier \f(CW\*(C`.*\*(C'\fR grabs as much of the string as possible
484 while still having the regex match. The second quantifier \f(CW\*(C`.*\*(C'\fR has
485 no string left to it, so it matches 0 times.
486 .SS "More matching"
487 .IX Subsection "More matching"
488 There are a few more things you might want to know about matching
489 operators.
490 The global modifier \f(CW\*(C`/g\*(C'\fR allows the matching operator to match
491 within a string as many times as possible. In scalar context,
492 successive matches against a string will have \f(CW\*(C`/g\*(C'\fR jump from match
493 to match, keeping track of position in the string as it goes along.
494 You can get or set the position with the \f(CWpos()\fR function.
495 For example,
496 .PP
497 .Vb 4
498 \& $x = "cat dog house"; # 3 words
499 \& while ($x =~ /(\ew+)/g) {
500 \& print "Word is $1, ends at position ", pos $x, "\en";
501 \& }
502 .Ve
503 .PP
504 prints
505 .PP
506 .Vb 3
507 \& Word is cat, ends at position 3
508 \& Word is dog, ends at position 7
509 \& Word is house, ends at position 13
510 .Ve
511 .PP
512 A failed match or changing the target string resets the position. If
513 you don\*(Aqt want the position reset after failure to match, add the
514 \&\f(CW\*(C`/c\*(C'\fR, as in \f(CW\*(C`/regex/gc\*(C'\fR.
515 .PP
516 In list context, \f(CW\*(C`/g\*(C'\fR returns a list of matched groupings, or if
517 there are no groupings, a list of matches to the whole regex. So
518 .PP
519 .Vb 4
520 \& @words = ($x =~ /(\ew+)/g); # matches,
521 \& # $word[0] = \*(Aqcat\*(Aq
522 \& # $word[1] = \*(Aqdog\*(Aq
523 \& # $word[2] = \*(Aqhouse\*(Aq
524 .Ve
525 .SS "Search and replace"
526 .IX Subsection "Search and replace"
527 Search and replace is performed using \f(CW\*(C`s/regex/replacement/modifiers\*(C'\fR.
528 The \f(CW\*(C`replacement\*(C'\fR is a Perl double\-quoted string that replaces in the
529 string whatever is matched with the \f(CW\*(C`regex\*(C'\fR. The operator \f(CW\*(C`=~\*(C'\fR is
530 also used here to associate a string with \f(CW\*(C`s///\*(C'\fR. If matching
531 against \f(CW$_\fR, the \f(CW\*(C`$_\ =~\*(C'\fR can be dropped. If there is a match,
532 \&\f(CW\*(C`s///\*(C'\fR returns the number of substitutions made; otherwise it returns
533 false. Here are a few examples:
534 .PP
535 .Vb 5
536 \& $x = "Time to feed the cat!";
537 \& $x =~ s/cat/hacker/; # $x contains "Time to feed the hacker!"
538 \& $y = "\*(Aqquoted words\*(Aq";
539 \& $y =~ s/^\*(Aq(.*)\*(Aq$/$1/; # strip single quotes,
540 \& # $y contains "quoted words"
541 .Ve
542 .PP
543 With the \f(CW\*(C`s///\*(C'\fR operator, the matched variables \f(CW$1\fR, \f(CW$2\fR, etc.
544 are immediately available for use in the replacement expression. With
545 the global modifier, \f(CW\*(C`s///g\*(C'\fR will search and replace all occurrences
546 of the regex in the string:
547 .PP
548 .Vb 4
549 \& $x = "I batted 4 for 4";
550 \& $x =~ s/4/four/; # $x contains "I batted four for 4"
551 \& $x = "I batted 4 for 4";
552 \& $x =~ s/4/four/g; # $x contains "I batted four for four"
553 .Ve
554 .PP
555 The non\-destructive modifier \f(CW\*(C`s///r\*(C'\fR causes the result of the substitution
556 to be returned instead of modifying \f(CW$_\fR (or whatever variable the
557 substitute was bound to with \f(CW\*(C`=~\*(C'\fR):
558 .PP
559 .Vb 3
560 \& $x = "I like dogs.";
561 \& $y = $x =~ s/dogs/cats/r;
562 \& print "$x $y\en"; # prints "I like dogs. I like cats."
563 \&
564 \& $x = "Cats are great.";
565 \& print $x =~ s/Cats/Dogs/r =~ s/Dogs/Frogs/r =~
566 \& s/Frogs/Hedgehogs/r, "\en";
567 \& # prints "Hedgehogs are great."
568 \&
569 \& @foo = map { s/[a\-z]/X/r } qw(a b c 1 2 3);
570 \& # @foo is now qw(X X X 1 2 3)
571 .Ve
572 .PP
573 The evaluation modifier \f(CW\*(C`s///e\*(C'\fR wraps an \f(CW\*(C`eval{...}\*(C'\fR around the
574 replacement string and the evaluated result is substituted for the
575 matched substring. Some examples:
576 .PP
577 .Vb 3
578 \& # reverse all the words in a string
579 \& $x = "the cat in the hat";
580 \& $x =~ s/(\ew+)/reverse $1/ge; # $x contains "eht tac ni eht tah"
581 \&
582 \& # convert percentage to decimal
583 \& $x = "A 39% hit rate";
584 \& $x =~ s!(\ed+)%!$1/100!e; # $x contains "A 0.39 hit rate"
585 .Ve
586 .PP
587 The last example shows that \f(CW\*(C`s///\*(C'\fR can use other delimiters, such as
588 \&\f(CW\*(C`s!!!\*(C'\fR and \f(CW\*(C`s{}{}\*(C'\fR, and even \f(CW\*(C`s{}//\*(C'\fR. If single quotes are used
589 \&\f(CW\*(C`s\*(Aq\*(Aq\*(Aq\*(C'\fR, then the regex and replacement are treated as single\-quoted
590 strings.
591 .SS "The split operator"
592 .IX Subsection "The split operator"
593 \&\f(CW\*(C`split /regex/, string\*(C'\fR splits \f(CW\*(C`string\*(C'\fR into a list of substrings
594 and returns that list. The regex determines the character sequence
595 that \f(CW\*(C`string\*(C'\fR is split with respect to. For example, to split a
596 string into words, use
597 .PP
598 .Vb 4
599 \& $x = "Calvin and Hobbes";
600 \& @word = split /\es+/, $x; # $word[0] = \*(AqCalvin\*(Aq
601 \& # $word[1] = \*(Aqand\*(Aq
602 \& # $word[2] = \*(AqHobbes\*(Aq
603 .Ve
604 .PP
605 To extract a comma\-delimited list of numbers, use
606 .PP
607 .Vb 4
608 \& $x = "1.618,2.718, 3.142";
609 \& @const = split /,\es*/, $x; # $const[0] = \*(Aq1.618\*(Aq
610 \& # $const[1] = \*(Aq2.718\*(Aq
611 \& # $const[2] = \*(Aq3.142\*(Aq
612 .Ve
613 .PP
614 If the empty regex \f(CW\*(C`//\*(C'\fR is used, the string is split into individual
615 characters. If the regex has groupings, then the list produced contains
616 the matched substrings from the groupings as well:
617 .PP
618 .Vb 6
619 \& $x = "/usr/bin";
620 \& @parts = split m!(/)!, $x; # $parts[0] = \*(Aq\*(Aq
621 \& # $parts[1] = \*(Aq/\*(Aq
622 \& # $parts[2] = \*(Aqusr\*(Aq
623 \& # $parts[3] = \*(Aq/\*(Aq
624 \& # $parts[4] = \*(Aqbin\*(Aq
625 .Ve
626 .PP
627 Since the first character of \f(CW$x\fR matched the regex, \f(CW\*(C`split\*(C'\fR prepended
628 an empty initial element to the list.
629 .ie n .SS """use re \*(Aqstrict\*(Aq"""
630 .el .SS "\f(CWuse re \*(Aqstrict\*(Aq\fP"
631 .IX Subsection "use re 'strict'"
632 New in v5.22, this applies stricter rules than otherwise when compiling
633 regular expression patterns. It can find things that, while legal, may
634 not be what you intended.
635 .PP
636 See \*(Aqstrict\*(Aq in re.
637 .SH BUGS
638 .IX Header "BUGS"
639 None.
640 .SH "SEE ALSO"
641 .IX Header "SEE ALSO"
642 This is just a quick start guide. For a more in\-depth tutorial on
643 regexes, see perlretut and for the reference page, see perlre.
644 .SH "AUTHOR AND COPYRIGHT"
645 .IX Header "AUTHOR AND COPYRIGHT"
646 Copyright (c) 2000 Mark Kvale
647 All rights reserved.
648 .PP
649 This document may be distributed under the same terms as Perl itself.
650 .SS Acknowledgments
651 .IX Subsection "Acknowledgments"
652 The author would like to thank Mark\-Jason Dominus, Tom Christiansen,
653 Ilya Zakharevich, Brad Hughes, and Mike Giroux for all their helpful
654 comments.