Commit | Line | Data |
a0d0e21e |
1 | =head1 NAME |
2 | |
3 | perlre - Perl regular expressions |
4 | |
5 | =head1 DESCRIPTION |
6 | |
cb1a09d0 |
7 | This page describes the syntax of regular expressions in Perl. For a |
5f05dabc |
8 | description of how to I<use> regular expressions in matching |
75e14d17 |
9 | operations, plus various examples of the same, see discussion |
1e66bd83 |
10 | of C<m//>, C<s///>, C<qr//> and C<??> in L<perlop/"Regexp Quote-Like Operators">. |
cb1a09d0 |
11 | |
68dc0745 |
12 | The matching operations can have various modifiers. The modifiers |
5a964f20 |
13 | that relate to the interpretation of the regular expression inside |
1e66bd83 |
14 | are listed below. For the modifiers that alter the way a regular expression |
15 | is used by Perl, see L<perlop/"Regexp Quote-Like Operators"> and |
16 | L<perlop/"Gory details of parsing quoted constructs">. |
a0d0e21e |
17 | |
55497cff |
18 | =over 4 |
19 | |
20 | =item i |
21 | |
22 | Do case-insensitive pattern matching. |
23 | |
a034a98d |
24 | If C<use locale> is in effect, the case map is taken from the current |
25 | locale. See L<perllocale>. |
26 | |
54310121 |
27 | =item m |
55497cff |
28 | |
29 | Treat string as multiple lines. That is, change "^" and "$" from matching |
5f05dabc |
30 | at only the very start or end of the string to the start or end of any |
55497cff |
31 | line anywhere within the string, |
32 | |
54310121 |
33 | =item s |
55497cff |
34 | |
35 | Treat string as single line. That is, change "." to match any character |
36 | whatsoever, even a newline, which it normally would not match. |
37 | |
5a964f20 |
38 | The C</s> and C</m> modifiers both override the C<$*> setting. That is, no matter |
39 | what C<$*> contains, C</s> without C</m> will force "^" to match only at the |
7b8d334a |
40 | beginning of the string and "$" to match only at the end (or just before a |
41 | newline at the end) of the string. Together, as /ms, they let the "." match |
42 | any character whatsoever, while yet allowing "^" and "$" to match, |
43 | respectively, just after and just before newlines within the string. |
44 | |
54310121 |
45 | =item x |
55497cff |
46 | |
47 | Extend your pattern's legibility by permitting whitespace and comments. |
48 | |
49 | =back |
a0d0e21e |
50 | |
51 | These are usually written as "the C</x> modifier", even though the delimiter |
52 | in question might not actually be a slash. In fact, any of these |
53 | modifiers may also be embedded within the regular expression itself using |
54 | the new C<(?...)> construct. See below. |
55 | |
4633a7c4 |
56 | The C</x> modifier itself needs a little more explanation. It tells |
55497cff |
57 | the regular expression parser to ignore whitespace that is neither |
58 | backslashed nor within a character class. You can use this to break up |
4633a7c4 |
59 | your regular expression into (slightly) more readable parts. The C<#> |
54310121 |
60 | character is also treated as a metacharacter introducing a comment, |
55497cff |
61 | just as in ordinary Perl code. This also means that if you want real |
5a964f20 |
62 | whitespace or C<#> characters in the pattern (outside of a character |
63 | class, where they are unaffected by C</x>), that you'll either have to |
55497cff |
64 | escape them or encode them using octal or hex escapes. Taken together, |
65 | these features go a long way towards making Perl's regular expressions |
0c815be9 |
66 | more readable. Note that you have to be careful not to include the |
67 | pattern delimiter in the comment--perl has no way of knowing you did |
5a964f20 |
68 | not intend to close the pattern early. See the C-comment deletion code |
0c815be9 |
69 | in L<perlop>. |
a0d0e21e |
70 | |
71 | =head2 Regular Expressions |
72 | |
73 | The patterns used in pattern matching are regular expressions such as |
5a964f20 |
74 | those supplied in the Version 8 regex routines. (In fact, the |
a0d0e21e |
75 | routines are derived (distantly) from Henry Spencer's freely |
76 | redistributable reimplementation of the V8 routines.) |
77 | See L<Version 8 Regular Expressions> for details. |
78 | |
79 | In particular the following metacharacters have their standard I<egrep>-ish |
80 | meanings: |
81 | |
54310121 |
82 | \ Quote the next metacharacter |
a0d0e21e |
83 | ^ Match the beginning of the line |
84 | . Match any character (except newline) |
c07a80fd |
85 | $ Match the end of the line (or before newline at the end) |
a0d0e21e |
86 | | Alternation |
87 | () Grouping |
88 | [] Character class |
89 | |
5f05dabc |
90 | By default, the "^" character is guaranteed to match at only the |
91 | beginning of the string, the "$" character at only the end (or before the |
a0d0e21e |
92 | newline at the end) and Perl does certain optimizations with the |
93 | assumption that the string contains only one line. Embedded newlines |
94 | will not be matched by "^" or "$". You may, however, wish to treat a |
4a6725af |
95 | string as a multi-line buffer, such that the "^" will match after any |
a0d0e21e |
96 | newline within the string, and "$" will match before any newline. At the |
97 | cost of a little more overhead, you can do this by using the /m modifier |
98 | on the pattern match operator. (Older programs did this by setting C<$*>, |
5f05dabc |
99 | but this practice is now deprecated.) |
a0d0e21e |
100 | |
4a6725af |
101 | To facilitate multi-line substitutions, the "." character never matches a |
55497cff |
102 | newline unless you use the C</s> modifier, which in effect tells Perl to pretend |
a0d0e21e |
103 | the string is a single line--even if it isn't. The C</s> modifier also |
104 | overrides the setting of C<$*>, in case you have some (badly behaved) older |
105 | code that sets it in another module. |
106 | |
107 | The following standard quantifiers are recognized: |
108 | |
109 | * Match 0 or more times |
110 | + Match 1 or more times |
111 | ? Match 1 or 0 times |
112 | {n} Match exactly n times |
113 | {n,} Match at least n times |
114 | {n,m} Match at least n but not more than m times |
115 | |
116 | (If a curly bracket occurs in any other context, it is treated |
117 | as a regular character.) The "*" modifier is equivalent to C<{0,}>, the "+" |
25f94b33 |
118 | modifier to C<{1,}>, and the "?" modifier to C<{0,1}>. n and m are limited |
c07a80fd |
119 | to integral values less than 65536. |
a0d0e21e |
120 | |
54310121 |
121 | By default, a quantified subpattern is "greedy", that is, it will match as |
122 | many times as possible (given a particular starting location) while still |
123 | allowing the rest of the pattern to match. If you want it to match the |
124 | minimum number of times possible, follow the quantifier with a "?". Note |
125 | that the meanings don't change, just the "greediness": |
a0d0e21e |
126 | |
127 | *? Match 0 or more times |
128 | +? Match 1 or more times |
129 | ?? Match 0 or 1 time |
130 | {n}? Match exactly n times |
131 | {n,}? Match at least n times |
132 | {n,m}? Match at least n but not more than m times |
133 | |
5f05dabc |
134 | Because patterns are processed as double quoted strings, the following |
a0d0e21e |
135 | also work: |
136 | |
0f36ee90 |
137 | \t tab (HT, TAB) |
138 | \n newline (LF, NL) |
139 | \r return (CR) |
140 | \f form feed (FF) |
141 | \a alarm (bell) (BEL) |
142 | \e escape (think troff) (ESC) |
cb1a09d0 |
143 | \033 octal char (think of a PDP-11) |
144 | \x1B hex char |
a0ed51b3 |
145 | \x{263a} wide hex char (Unicode SMILEY) |
a0d0e21e |
146 | \c[ control char |
cb1a09d0 |
147 | \l lowercase next char (think vi) |
148 | \u uppercase next char (think vi) |
149 | \L lowercase till \E (think vi) |
150 | \U uppercase till \E (think vi) |
151 | \E end case modification (think vi) |
5a964f20 |
152 | \Q quote (disable) pattern metacharacters till \E |
a0d0e21e |
153 | |
a034a98d |
154 | If C<use locale> is in effect, the case map used by C<\l>, C<\L>, C<\u> |
7b8d334a |
155 | and C<\U> is taken from the current locale. See L<perllocale>. |
a034a98d |
156 | |
1d2dff63 |
157 | You cannot include a literal C<$> or C<@> within a C<\Q> sequence. |
158 | An unescaped C<$> or C<@> interpolates the corresponding variable, |
159 | while escaping will cause the literal string C<\$> to be matched. |
160 | You'll need to write something like C<m/\Quser\E\@\Qhost/>. |
161 | |
a0d0e21e |
162 | In addition, Perl defines the following: |
163 | |
164 | \w Match a "word" character (alphanumeric plus "_") |
165 | \W Match a non-word character |
166 | \s Match a whitespace character |
167 | \S Match a non-whitespace character |
168 | \d Match a digit character |
169 | \D Match a non-digit character |
a0ed51b3 |
170 | \pP Match P, named property. Use \p{Prop} for longer names. |
171 | \PP Match non-P |
f244e06d |
172 | \X Match eXtended Unicode "combining character sequence", |
173 | equivalent to C<(?:\PM\pM*)> |
a0ed51b3 |
174 | \C Match a single C char (octet) even under utf8. |
a0d0e21e |
175 | |
5a964f20 |
176 | A C<\w> matches a single alphanumeric character, not a whole |
a034a98d |
177 | word. To match a word you'd need to say C<\w+>. If C<use locale> is in |
178 | effect, the list of alphabetic characters generated by C<\w> is taken |
179 | from the current locale. See L<perllocale>. You may use C<\w>, C<\W>, |
180 | C<\s>, C<\S>, C<\d>, and C<\D> within character classes (though not as |
181 | either end of a range). |
a0d0e21e |
182 | |
183 | Perl defines the following zero-width assertions: |
184 | |
185 | \b Match a word boundary |
186 | \B Match a non-(word boundary) |
b85d18e9 |
187 | \A Match only at beginning of string |
188 | \Z Match only at end of string, or before newline at the end |
189 | \z Match only at end of string |
a99df21c |
190 | \G Match only where previous m//g left off (works only with /g) |
a0d0e21e |
191 | |
192 | A word boundary (C<\b>) is defined as a spot between two characters that |
68dc0745 |
193 | has a C<\w> on one side of it and a C<\W> on the other side of it (in |
a0d0e21e |
194 | either order), counting the imaginary characters off the beginning and |
195 | end of the string as matching a C<\W>. (Within character classes C<\b> |
196 | represents backspace rather than a word boundary.) The C<\A> and C<\Z> are |
5a964f20 |
197 | just like "^" and "$", except that they won't match multiple times when the |
a0d0e21e |
198 | C</m> modifier is used, while "^" and "$" will match at every internal line |
c07a80fd |
199 | boundary. To match the actual end of the string, not ignoring newline, |
b85d18e9 |
200 | you can use C<\z>. The C<\G> assertion can be used to chain global |
a99df21c |
201 | matches (using C<m//g>), as described in |
e7ea3e70 |
202 | L<perlop/"Regexp Quote-Like Operators">. |
a99df21c |
203 | |
e7ea3e70 |
204 | It is also useful when writing C<lex>-like scanners, when you have several |
5a964f20 |
205 | patterns that you want to match against consequent substrings of your |
e7ea3e70 |
206 | string, see the previous reference. |
44a8e56a |
207 | The actual location where C<\G> will match can also be influenced |
208 | by using C<pos()> as an lvalue. See L<perlfunc/pos>. |
a0d0e21e |
209 | |
0f36ee90 |
210 | When the bracketing construct C<( ... )> is used, \E<lt>digitE<gt> matches the |
cb1a09d0 |
211 | digit'th substring. Outside of the pattern, always use "$" instead of "\" |
0f36ee90 |
212 | in front of the digit. (While the \E<lt>digitE<gt> notation can on rare occasion work |
cb1a09d0 |
213 | outside the current pattern, this should not be relied upon. See the |
0f36ee90 |
214 | WARNING below.) The scope of $E<lt>digitE<gt> (and C<$`>, C<$&>, and C<$'>) |
cb1a09d0 |
215 | extends to the end of the enclosing BLOCK or eval string, or to the next |
216 | successful pattern match, whichever comes first. If you want to use |
5f05dabc |
217 | parentheses to delimit a subpattern (e.g., a set of alternatives) without |
84dc3c4d |
218 | saving it as a subpattern, follow the ( with a ?:. |
cb1a09d0 |
219 | |
220 | You may have as many parentheses as you wish. If you have more |
a0d0e21e |
221 | than 9 substrings, the variables $10, $11, ... refer to the |
222 | corresponding substring. Within the pattern, \10, \11, etc. refer back |
5f05dabc |
223 | to substrings if there have been at least that many left parentheses before |
c07a80fd |
224 | the backreference. Otherwise (for backward compatibility) \10 is the |
a0d0e21e |
225 | same as \010, a backspace, and \11 the same as \011, a tab. And so |
226 | on. (\1 through \9 are always backreferences.) |
227 | |
228 | C<$+> returns whatever the last bracket match matched. C<$&> returns the |
0f36ee90 |
229 | entire matched string. (C<$0> used to return the same thing, but not any |
a0d0e21e |
230 | more.) C<$`> returns everything before the matched string. C<$'> returns |
231 | everything after the matched string. Examples: |
232 | |
233 | s/^([^ ]*) *([^ ]*)/$2 $1/; # swap first two words |
234 | |
235 | if (/Time: (..):(..):(..)/) { |
236 | $hours = $1; |
237 | $minutes = $2; |
238 | $seconds = $3; |
239 | } |
240 | |
68dc0745 |
241 | Once perl sees that you need one of C<$&>, C<$`> or C<$'> anywhere in |
242 | the program, it has to provide them on each and every pattern match. |
243 | This can slow your program down. The same mechanism that handles |
244 | these provides for the use of $1, $2, etc., so you pay the same price |
5a964f20 |
245 | for each pattern that contains capturing parentheses. But if you never |
246 | use $&, etc., in your script, then patterns I<without> capturing |
68dc0745 |
247 | parentheses won't be penalized. So avoid $&, $', and $` if you can, |
248 | but if you can't (and some algorithms really appreciate them), once |
249 | you've used them once, use them at will, because you've already paid |
5a964f20 |
250 | the price. As of 5.005, $& is not so costly as the other two. |
68dc0745 |
251 | |
5a964f20 |
252 | Backslashed metacharacters in Perl are |
201ecf35 |
253 | alphanumeric, such as C<\b>, C<\w>, C<\n>. Unlike some other regular |
254 | expression languages, there are no backslashed symbols that aren't |
255 | alphanumeric. So anything that looks like \\, \(, \), \E<lt>, \E<gt>, |
256 | \{, or \} is always interpreted as a literal character, not a |
257 | metacharacter. This was once used in a common idiom to disable or |
258 | quote the special meanings of regular expression metacharacters in a |
5a964f20 |
259 | string that you want to use for a pattern. Simply quote all |
a0d0e21e |
260 | non-alphanumeric characters: |
261 | |
262 | $pattern =~ s/(\W)/\\$1/g; |
263 | |
201ecf35 |
264 | Now it is much more common to see either the quotemeta() function or |
7b8d334a |
265 | the C<\Q> escape sequence used to disable all metacharacters' special |
201ecf35 |
266 | meanings like this: |
a0d0e21e |
267 | |
268 | /$unquoted\Q$quoted\E$unquoted/ |
269 | |
5f05dabc |
270 | Perl defines a consistent extension syntax for regular expressions. |
271 | The syntax is a pair of parentheses with a question mark as the first |
272 | thing within the parentheses (this was a syntax error in older |
273 | versions of Perl). The character after the question mark gives the |
274 | function of the extension. Several extensions are already supported: |
a0d0e21e |
275 | |
276 | =over 10 |
277 | |
cc6b7395 |
278 | =item C<(?#text)> |
a0d0e21e |
279 | |
cb1a09d0 |
280 | A comment. The text is ignored. If the C</x> switch is used to enable |
259138e3 |
281 | whitespace formatting, a simple C<#> will suffice. Note that perl closes |
282 | the comment as soon as it sees a C<)>, so there is no way to put a literal |
283 | C<)> in the comment. |
a0d0e21e |
284 | |
5a964f20 |
285 | =item C<(?:pattern)> |
a0d0e21e |
286 | |
ca9dfc88 |
287 | =item C<(?imsx-imsx:pattern)> |
288 | |
5a964f20 |
289 | This is for clustering, not capturing; it groups subexpressions like |
290 | "()", but doesn't make backreferences as "()" does. So |
a0d0e21e |
291 | |
5a964f20 |
292 | @fields = split(/\b(?:a|b|c)\b/) |
a0d0e21e |
293 | |
294 | is like |
295 | |
5a964f20 |
296 | @fields = split(/\b(a|b|c)\b/) |
a0d0e21e |
297 | |
298 | but doesn't spit out extra fields. |
299 | |
ca9dfc88 |
300 | The letters between C<?> and C<:> act as flags modifiers, see |
301 | L<C<(?imsx-imsx)>>. In particular, |
302 | |
303 | /(?s-i:more.*than).*million/i |
304 | |
305 | is equivalent to more verbose |
306 | |
307 | /(?:(?s-i)more.*than).*million/i |
308 | |
5a964f20 |
309 | =item C<(?=pattern)> |
a0d0e21e |
310 | |
311 | A zero-width positive lookahead assertion. For example, C</\w+(?=\t)/> |
312 | matches a word followed by a tab, without including the tab in C<$&>. |
313 | |
5a964f20 |
314 | =item C<(?!pattern)> |
a0d0e21e |
315 | |
316 | A zero-width negative lookahead assertion. For example C</foo(?!bar)/> |
317 | matches any occurrence of "foo" that isn't followed by "bar". Note |
318 | however that lookahead and lookbehind are NOT the same thing. You cannot |
7b8d334a |
319 | use this for lookbehind. |
320 | |
5a964f20 |
321 | If you are looking for a "bar" that isn't preceded by a "foo", C</(?!foo)bar/> |
7b8d334a |
322 | will not do what you want. That's because the C<(?!foo)> is just saying that |
323 | the next thing cannot be "foo"--and it's not, it's a "bar", so "foobar" will |
324 | match. You would have to do something like C</(?!foo)...bar/> for that. We |
325 | say "like" because there's the case of your "bar" not having three characters |
326 | before it. You could cover that this way: C</(?:(?!foo)...|^.{0,2})bar/>. |
327 | Sometimes it's still easier just to say: |
a0d0e21e |
328 | |
a3cb178b |
329 | if (/bar/ && $` !~ /foo$/) |
a0d0e21e |
330 | |
c277df42 |
331 | For lookbehind see below. |
332 | |
5a964f20 |
333 | =item C<(?E<lt>=pattern)> |
c277df42 |
334 | |
5a964f20 |
335 | A zero-width positive lookbehind assertion. For example, C</(?E<lt>=\t)\w+/> |
c277df42 |
336 | matches a word following a tab, without including the tab in C<$&>. |
337 | Works only for fixed-width lookbehind. |
338 | |
5a964f20 |
339 | =item C<(?<!pattern)> |
c277df42 |
340 | |
341 | A zero-width negative lookbehind assertion. For example C</(?<!bar)foo/> |
342 | matches any occurrence of "foo" that isn't following "bar". |
343 | Works only for fixed-width lookbehind. |
344 | |
cc6b7395 |
345 | =item C<(?{ code })> |
c277df42 |
346 | |
347 | Experimental "evaluate any Perl code" zero-width assertion. Always |
cc6b7395 |
348 | succeeds. C<code> is not interpolated. Currently the rules to |
349 | determine where the C<code> ends are somewhat convoluted. |
c277df42 |
350 | |
b9ac3b5b |
351 | The C<code> is properly scoped in the following sense: if the assertion |
352 | is backtracked (compare L<"Backtracking">), all the changes introduced after |
353 | C<local>isation are undone, so |
354 | |
355 | $_ = 'a' x 8; |
356 | m< |
357 | (?{ $cnt = 0 }) # Initialize $cnt. |
358 | ( |
359 | a |
360 | (?{ |
361 | local $cnt = $cnt + 1; # Update $cnt, backtracking-safe. |
362 | }) |
363 | )* |
364 | aaaa |
365 | (?{ $res = $cnt }) # On success copy to non-localized |
366 | # location. |
367 | >x; |
368 | |
369 | will set C<$res = 4>. Note that after the match $cnt returns to the globally |
370 | introduced value 0, since the scopes which restrict C<local> statements |
371 | are unwound. |
372 | |
373 | This assertion may be used as L<C<(?(condition)yes-pattern|no-pattern)>> |
374 | switch. If I<not> used in this way, the result of evaluation of C<code> |
375 | is put into variable $^R. This happens immediately, so $^R can be used from |
376 | other C<(?{ code })> assertions inside the same regular expression. |
377 | |
378 | The above assignment to $^R is properly localized, thus the old value of $^R |
379 | is restored if the assertion is backtracked (compare L<"Backtracking">). |
380 | |
871b0233 |
381 | Due to security concerns, this construction is not allowed if the regular |
382 | expression involves run-time interpolation of variables, unless |
383 | C<use re 'eval'> pragma is used (see L<re>), or the variables contain |
384 | results of qr() operator (see L<perlop/"qr/STRING/imosx">). |
385 | |
386 | This restriction is due to the wide-spread (questionable) practice of |
387 | using the construct |
388 | |
389 | $re = <>; |
390 | chomp $re; |
391 | $string =~ /$re/; |
392 | |
393 | without tainting. While this code is frowned upon from security point |
394 | of view, when C<(?{})> was introduced, it was considered bad to add |
395 | I<new> security holes to existing scripts. |
396 | |
397 | B<NOTE:> Use of the above insecure snippet without also enabling taint mode |
398 | is to be severely frowned upon. C<use re 'eval'> does not disable tainting |
399 | checks, thus to allow $re in the above snippet to contain C<(?{})> |
400 | I<with tainting enabled>, one needs both C<use re 'eval'> and untaint |
401 | the $re. |
402 | |
0f5d15d6 |
403 | =item C<(?p{ code })> |
404 | |
405 | I<Very experimental> "postponed" regular subexpression. C<code> is evaluated |
406 | at runtime, at the moment this subexpression may match. The result of |
407 | evaluation is considered as a regular expression, and matched as if it |
408 | were inserted instead of this construct. |
409 | |
410 | C<code> is not interpolated. Currently the rules to |
411 | determine where the C<code> ends are somewhat convoluted. |
412 | |
413 | The following regular expression matches matching parenthesized group: |
414 | |
415 | $re = qr{ |
416 | \( |
417 | (?: |
418 | (?> [^()]+ ) # Non-parens without backtracking |
419 | | |
420 | (?p{ $re }) # Group with matching parens |
421 | )* |
422 | \) |
423 | }x; |
424 | |
5a964f20 |
425 | =item C<(?E<gt>pattern)> |
426 | |
427 | An "independent" subexpression. Matches the substring that a |
428 | I<standalone> C<pattern> would match if anchored at the given position, |
c277df42 |
429 | B<and only this substring>. |
430 | |
431 | Say, C<^(?E<gt>a*)ab> will never match, since C<(?E<gt>a*)> (anchored |
5a964f20 |
432 | at the beginning of string, as above) will match I<all> characters |
c277df42 |
433 | C<a> at the beginning of string, leaving no C<a> for C<ab> to match. |
434 | In contrast, C<a*ab> will match the same as C<a+b>, since the match of |
435 | the subgroup C<a*> is influenced by the following group C<ab> (see |
436 | L<"Backtracking">). In particular, C<a*> inside C<a*ab> will match |
aca73f04 |
437 | fewer characters than a standalone C<a*>, since this makes the tail match. |
c277df42 |
438 | |
5a964f20 |
439 | An effect similar to C<(?E<gt>pattern)> may be achieved by |
c277df42 |
440 | |
5a964f20 |
441 | (?=(pattern))\1 |
c277df42 |
442 | |
443 | since the lookahead is in I<"logical"> context, thus matches the same |
444 | substring as a standalone C<a+>. The following C<\1> eats the matched |
445 | string, thus making a zero-length assertion into an analogue of |
871b0233 |
446 | C<(?E<gt>...)>. (The difference between these two constructs is that the |
5a964f20 |
447 | second one uses a catching group, thus shifting ordinals of |
c277df42 |
448 | backreferences in the rest of a regular expression.) |
449 | |
5a964f20 |
450 | This construct is useful for optimizations of "eternal" |
451 | matches, because it will not backtrack (see L<"Backtracking">). |
c277df42 |
452 | |
871b0233 |
453 | m{ \( |
454 | ( |
455 | [^()]+ |
456 | | |
457 | \( [^()]* \) |
458 | )+ |
459 | \) |
460 | }x |
5a964f20 |
461 | |
462 | That will efficiently match a nonempty group with matching |
463 | two-or-less-level-deep parentheses. However, if there is no such group, |
464 | it will take virtually forever on a long string. That's because there are |
465 | so many different ways to split a long string into several substrings. |
871b0233 |
466 | This is what C<(.+)+> is doing, and C<(.+)+> is similar to a subpattern |
467 | of the above pattern. Consider that the above pattern detects no-match |
468 | on C<((()aaaaaaaaaaaaaaaaaa> in several seconds, but that each extra |
5a964f20 |
469 | letter doubles this time. This exponential performance will make it |
470 | appear that your program has hung. |
471 | |
472 | However, a tiny modification of this pattern |
473 | |
871b0233 |
474 | m{ \( |
475 | ( |
476 | (?> [^()]+ ) |
477 | | |
478 | \( [^()]* \) |
479 | )+ |
480 | \) |
481 | }x |
c277df42 |
482 | |
5a964f20 |
483 | which uses C<(?E<gt>...)> matches exactly when the one above does (verifying |
484 | this yourself would be a productive exercise), but finishes in a fourth |
485 | the time when used on a similar string with 1000000 C<a>s. Be aware, |
486 | however, that this pattern currently triggers a warning message under |
487 | B<-w> saying it C<"matches the null string many times">): |
c277df42 |
488 | |
8d300b32 |
489 | On simple groups, such as the pattern C<(?E<gt> [^()]+ )>, a comparable |
c277df42 |
490 | effect may be achieved by negative lookahead, as in C<[^()]+ (?! [^()] )>. |
491 | This was only 4 times slower on a string with 1000000 C<a>s. |
492 | |
5a964f20 |
493 | =item C<(?(condition)yes-pattern|no-pattern)> |
c277df42 |
494 | |
5a964f20 |
495 | =item C<(?(condition)yes-pattern)> |
c277df42 |
496 | |
497 | Conditional expression. C<(condition)> should be either an integer in |
498 | parentheses (which is valid if the corresponding pair of parentheses |
499 | matched), or lookahead/lookbehind/evaluate zero-width assertion. |
500 | |
501 | Say, |
502 | |
5a964f20 |
503 | m{ ( \( )? |
871b0233 |
504 | [^()]+ |
5a964f20 |
505 | (?(1) \) ) |
871b0233 |
506 | }x |
c277df42 |
507 | |
508 | matches a chunk of non-parentheses, possibly included in parentheses |
509 | themselves. |
a0d0e21e |
510 | |
ca9dfc88 |
511 | =item C<(?imsx-imsx)> |
a0d0e21e |
512 | |
513 | One or more embedded pattern-match modifiers. This is particularly |
514 | useful for patterns that are specified in a table somewhere, some of |
515 | which want to be case sensitive, and some of which don't. The case |
5f05dabc |
516 | insensitive ones need to include merely C<(?i)> at the front of the |
a0d0e21e |
517 | pattern. For example: |
518 | |
519 | $pattern = "foobar"; |
5a964f20 |
520 | if ( /$pattern/i ) { } |
a0d0e21e |
521 | |
522 | # more flexible: |
523 | |
524 | $pattern = "(?i)foobar"; |
5a964f20 |
525 | if ( /$pattern/ ) { } |
a0d0e21e |
526 | |
ca9dfc88 |
527 | Letters after C<-> switch modifiers off. |
528 | |
5a964f20 |
529 | These modifiers are localized inside an enclosing group (if any). Say, |
c277df42 |
530 | |
531 | ( (?i) blah ) \s+ \1 |
532 | |
533 | (assuming C<x> modifier, and no C<i> modifier outside of this group) |
534 | will match a repeated (I<including the case>!) word C<blah> in any |
535 | case. |
536 | |
a0d0e21e |
537 | =back |
538 | |
5a964f20 |
539 | A question mark was chosen for this and for the new minimal-matching |
540 | construct because 1) question mark is pretty rare in older regular |
541 | expressions, and 2) whenever you see one, you should stop and "question" |
542 | exactly what is going on. That's psychology... |
a0d0e21e |
543 | |
c07a80fd |
544 | =head2 Backtracking |
545 | |
c277df42 |
546 | A fundamental feature of regular expression matching involves the |
5a964f20 |
547 | notion called I<backtracking>, which is currently used (when needed) |
c277df42 |
548 | by all regular expression quantifiers, namely C<*>, C<*?>, C<+>, |
549 | C<+?>, C<{n,m}>, and C<{n,m}?>. |
c07a80fd |
550 | |
551 | For a regular expression to match, the I<entire> regular expression must |
552 | match, not just part of it. So if the beginning of a pattern containing a |
553 | quantifier succeeds in a way that causes later parts in the pattern to |
554 | fail, the matching engine backs up and recalculates the beginning |
555 | part--that's why it's called backtracking. |
556 | |
557 | Here is an example of backtracking: Let's say you want to find the |
558 | word following "foo" in the string "Food is on the foo table.": |
559 | |
560 | $_ = "Food is on the foo table."; |
561 | if ( /\b(foo)\s+(\w+)/i ) { |
562 | print "$2 follows $1.\n"; |
563 | } |
564 | |
565 | When the match runs, the first part of the regular expression (C<\b(foo)>) |
566 | finds a possible match right at the beginning of the string, and loads up |
567 | $1 with "Foo". However, as soon as the matching engine sees that there's |
568 | no whitespace following the "Foo" that it had saved in $1, it realizes its |
68dc0745 |
569 | mistake and starts over again one character after where it had the |
c07a80fd |
570 | tentative match. This time it goes all the way until the next occurrence |
571 | of "foo". The complete regular expression matches this time, and you get |
572 | the expected output of "table follows foo." |
573 | |
574 | Sometimes minimal matching can help a lot. Imagine you'd like to match |
575 | everything between "foo" and "bar". Initially, you write something |
576 | like this: |
577 | |
578 | $_ = "The food is under the bar in the barn."; |
579 | if ( /foo(.*)bar/ ) { |
580 | print "got <$1>\n"; |
581 | } |
582 | |
583 | Which perhaps unexpectedly yields: |
584 | |
585 | got <d is under the bar in the > |
586 | |
587 | That's because C<.*> was greedy, so you get everything between the |
588 | I<first> "foo" and the I<last> "bar". In this case, it's more effective |
589 | to use minimal matching to make sure you get the text between a "foo" |
590 | and the first "bar" thereafter. |
591 | |
592 | if ( /foo(.*?)bar/ ) { print "got <$1>\n" } |
593 | got <d is under the > |
594 | |
595 | Here's another example: let's say you'd like to match a number at the end |
596 | of a string, and you also want to keep the preceding part the match. |
597 | So you write this: |
598 | |
599 | $_ = "I have 2 numbers: 53147"; |
600 | if ( /(.*)(\d*)/ ) { # Wrong! |
601 | print "Beginning is <$1>, number is <$2>.\n"; |
602 | } |
603 | |
604 | That won't work at all, because C<.*> was greedy and gobbled up the |
605 | whole string. As C<\d*> can match on an empty string the complete |
606 | regular expression matched successfully. |
607 | |
8e1088bc |
608 | Beginning is <I have 2 numbers: 53147>, number is <>. |
c07a80fd |
609 | |
610 | Here are some variants, most of which don't work: |
611 | |
612 | $_ = "I have 2 numbers: 53147"; |
613 | @pats = qw{ |
614 | (.*)(\d*) |
615 | (.*)(\d+) |
616 | (.*?)(\d*) |
617 | (.*?)(\d+) |
618 | (.*)(\d+)$ |
619 | (.*?)(\d+)$ |
620 | (.*)\b(\d+)$ |
621 | (.*\D)(\d+)$ |
622 | }; |
623 | |
624 | for $pat (@pats) { |
625 | printf "%-12s ", $pat; |
626 | if ( /$pat/ ) { |
627 | print "<$1> <$2>\n"; |
628 | } else { |
629 | print "FAIL\n"; |
630 | } |
631 | } |
632 | |
633 | That will print out: |
634 | |
635 | (.*)(\d*) <I have 2 numbers: 53147> <> |
636 | (.*)(\d+) <I have 2 numbers: 5314> <7> |
637 | (.*?)(\d*) <> <> |
638 | (.*?)(\d+) <I have > <2> |
639 | (.*)(\d+)$ <I have 2 numbers: 5314> <7> |
640 | (.*?)(\d+)$ <I have 2 numbers: > <53147> |
641 | (.*)\b(\d+)$ <I have 2 numbers: > <53147> |
642 | (.*\D)(\d+)$ <I have 2 numbers: > <53147> |
643 | |
644 | As you see, this can be a bit tricky. It's important to realize that a |
645 | regular expression is merely a set of assertions that gives a definition |
646 | of success. There may be 0, 1, or several different ways that the |
647 | definition might succeed against a particular string. And if there are |
5a964f20 |
648 | multiple ways it might succeed, you need to understand backtracking to |
649 | know which variety of success you will achieve. |
c07a80fd |
650 | |
651 | When using lookahead assertions and negations, this can all get even |
54310121 |
652 | tricker. Imagine you'd like to find a sequence of non-digits not |
c07a80fd |
653 | followed by "123". You might try to write that as |
654 | |
871b0233 |
655 | $_ = "ABC123"; |
656 | if ( /^\D*(?!123)/ ) { # Wrong! |
657 | print "Yup, no 123 in $_\n"; |
658 | } |
c07a80fd |
659 | |
660 | But that isn't going to match; at least, not the way you're hoping. It |
661 | claims that there is no 123 in the string. Here's a clearer picture of |
662 | why it that pattern matches, contrary to popular expectations: |
663 | |
664 | $x = 'ABC123' ; |
665 | $y = 'ABC445' ; |
666 | |
667 | print "1: got $1\n" if $x =~ /^(ABC)(?!123)/ ; |
668 | print "2: got $1\n" if $y =~ /^(ABC)(?!123)/ ; |
669 | |
670 | print "3: got $1\n" if $x =~ /^(\D*)(?!123)/ ; |
671 | print "4: got $1\n" if $y =~ /^(\D*)(?!123)/ ; |
672 | |
673 | This prints |
674 | |
675 | 2: got ABC |
676 | 3: got AB |
677 | 4: got ABC |
678 | |
5f05dabc |
679 | You might have expected test 3 to fail because it seems to a more |
c07a80fd |
680 | general purpose version of test 1. The important difference between |
681 | them is that test 3 contains a quantifier (C<\D*>) and so can use |
682 | backtracking, whereas test 1 will not. What's happening is |
683 | that you've asked "Is it true that at the start of $x, following 0 or more |
5f05dabc |
684 | non-digits, you have something that's not 123?" If the pattern matcher had |
c07a80fd |
685 | let C<\D*> expand to "ABC", this would have caused the whole pattern to |
54310121 |
686 | fail. |
c07a80fd |
687 | The search engine will initially match C<\D*> with "ABC". Then it will |
5a964f20 |
688 | try to match C<(?!123> with "123", which of course fails. But because |
c07a80fd |
689 | a quantifier (C<\D*>) has been used in the regular expression, the |
690 | search engine can backtrack and retry the match differently |
54310121 |
691 | in the hope of matching the complete regular expression. |
c07a80fd |
692 | |
5a964f20 |
693 | The pattern really, I<really> wants to succeed, so it uses the |
694 | standard pattern back-off-and-retry and lets C<\D*> expand to just "AB" this |
c07a80fd |
695 | time. Now there's indeed something following "AB" that is not |
696 | "123". It's in fact "C123", which suffices. |
697 | |
698 | We can deal with this by using both an assertion and a negation. We'll |
699 | say that the first part in $1 must be followed by a digit, and in fact, it |
700 | must also be followed by something that's not "123". Remember that the |
701 | lookaheads are zero-width expressions--they only look, but don't consume |
702 | any of the string in their match. So rewriting this way produces what |
703 | you'd expect; that is, case 5 will fail, but case 6 succeeds: |
704 | |
705 | print "5: got $1\n" if $x =~ /^(\D*)(?=\d)(?!123)/ ; |
706 | print "6: got $1\n" if $y =~ /^(\D*)(?=\d)(?!123)/ ; |
707 | |
708 | 6: got ABC |
709 | |
5a964f20 |
710 | In other words, the two zero-width assertions next to each other work as though |
c07a80fd |
711 | they're ANDed together, just as you'd use any builtin assertions: C</^$/> |
712 | matches only if you're at the beginning of the line AND the end of the |
713 | line simultaneously. The deeper underlying truth is that juxtaposition in |
714 | regular expressions always means AND, except when you write an explicit OR |
715 | using the vertical bar. C</ab/> means match "a" AND (then) match "b", |
716 | although the attempted matches are made at different positions because "a" |
717 | is not a zero-width assertion, but a one-width assertion. |
718 | |
719 | One warning: particularly complicated regular expressions can take |
720 | exponential time to solve due to the immense number of possible ways they |
721 | can use backtracking to try match. For example this will take a very long |
722 | time to run |
723 | |
724 | /((a{0,5}){0,5}){0,5}/ |
725 | |
726 | And if you used C<*>'s instead of limiting it to 0 through 5 matches, then |
727 | it would take literally forever--or until you ran out of stack space. |
728 | |
c277df42 |
729 | A powerful tool for optimizing such beasts is "independent" groups, |
5a964f20 |
730 | which do not backtrace (see L<C<(?E<gt>pattern)>>). Note also that |
c277df42 |
731 | zero-length lookahead/lookbehind assertions will not backtrace to make |
732 | the tail match, since they are in "logical" context: only the fact |
733 | whether they match or not is considered relevant. For an example |
734 | where side-effects of a lookahead I<might> have influenced the |
5a964f20 |
735 | following match, see L<C<(?E<gt>pattern)>>. |
c277df42 |
736 | |
a0d0e21e |
737 | =head2 Version 8 Regular Expressions |
738 | |
5a964f20 |
739 | In case you're not familiar with the "regular" Version 8 regex |
a0d0e21e |
740 | routines, here are the pattern-matching rules not described above. |
741 | |
54310121 |
742 | Any single character matches itself, unless it is a I<metacharacter> |
a0d0e21e |
743 | with a special meaning described here or above. You can cause |
5a964f20 |
744 | characters that normally function as metacharacters to be interpreted |
5f05dabc |
745 | literally by prefixing them with a "\" (e.g., "\." matches a ".", not any |
a0d0e21e |
746 | character; "\\" matches a "\"). A series of characters matches that |
747 | series of characters in the target string, so the pattern C<blurfl> |
748 | would match "blurfl" in the target string. |
749 | |
750 | You can specify a character class, by enclosing a list of characters |
5a964f20 |
751 | in C<[]>, which will match any one character from the list. If the |
a0d0e21e |
752 | first character after the "[" is "^", the class matches any character not |
753 | in the list. Within a list, the "-" character is used to specify a |
5a964f20 |
754 | range, so that C<a-z> represents all characters between "a" and "z", |
84850974 |
755 | inclusive. If you want "-" itself to be a member of a class, put it |
756 | at the start or end of the list, or escape it with a backslash. (The |
757 | following all specify the same class of three characters: C<[-az]>, |
758 | C<[az-]>, and C<[a\-z]>. All are different from C<[a-z]>, which |
759 | specifies a class containing twenty-six characters.) |
a0d0e21e |
760 | |
8ada0baa |
761 | Note also that the whole range idea is rather unportable between |
762 | character sets--and even within character sets they may cause results |
763 | you probably didn't expect. A sound principle is to use only ranges |
764 | that begin from and end at either alphabets of equal case ([a-e], |
765 | [A-E]), or digits ([0-9]). Anything else is unsafe. If in doubt, |
766 | spell out the character sets in full. |
767 | |
54310121 |
768 | Characters may be specified using a metacharacter syntax much like that |
a0d0e21e |
769 | used in C: "\n" matches a newline, "\t" a tab, "\r" a carriage return, |
770 | "\f" a form feed, etc. More generally, \I<nnn>, where I<nnn> is a string |
771 | of octal digits, matches the character whose ASCII value is I<nnn>. |
0f36ee90 |
772 | Similarly, \xI<nn>, where I<nn> are hexadecimal digits, matches the |
a0d0e21e |
773 | character whose ASCII value is I<nn>. The expression \cI<x> matches the |
54310121 |
774 | ASCII character control-I<x>. Finally, the "." metacharacter matches any |
a0d0e21e |
775 | character except "\n" (unless you use C</s>). |
776 | |
777 | You can specify a series of alternatives for a pattern using "|" to |
778 | separate them, so that C<fee|fie|foe> will match any of "fee", "fie", |
5a964f20 |
779 | or "foe" in the target string (as would C<f(e|i|o)e>). The |
a0d0e21e |
780 | first alternative includes everything from the last pattern delimiter |
781 | ("(", "[", or the beginning of the pattern) up to the first "|", and |
782 | the last alternative contains everything from the last "|" to the next |
783 | pattern delimiter. For this reason, it's common practice to include |
784 | alternatives in parentheses, to minimize confusion about where they |
a3cb178b |
785 | start and end. |
786 | |
5a964f20 |
787 | Alternatives are tried from left to right, so the first |
a3cb178b |
788 | alternative found for which the entire expression matches, is the one that |
789 | is chosen. This means that alternatives are not necessarily greedy. For |
790 | example: when mathing C<foo|foot> against "barefoot", only the "foo" |
791 | part will match, as that is the first alternative tried, and it successfully |
792 | matches the target string. (This might not seem important, but it is |
793 | important when you are capturing matched text using parentheses.) |
794 | |
5a964f20 |
795 | Also remember that "|" is interpreted as a literal within square brackets, |
a3cb178b |
796 | so if you write C<[fee|fie|foe]> you're really only matching C<[feio|]>. |
a0d0e21e |
797 | |
54310121 |
798 | Within a pattern, you may designate subpatterns for later reference by |
a0d0e21e |
799 | enclosing them in parentheses, and you may refer back to the I<n>th |
54310121 |
800 | subpattern later in the pattern using the metacharacter \I<n>. |
801 | Subpatterns are numbered based on the left to right order of their |
5a964f20 |
802 | opening parenthesis. A backreference matches whatever |
54310121 |
803 | actually matched the subpattern in the string being examined, not the |
804 | rules for that subpattern. Therefore, C<(0|0x)\d*\s\1\d*> will |
5a964f20 |
805 | match "0x1234 0x4321", but not "0x1234 01234", because subpattern 1 |
748a9306 |
806 | actually matched "0x", even though the rule C<0|0x> could |
a0d0e21e |
807 | potentially match the leading 0 in the second number. |
cb1a09d0 |
808 | |
809 | =head2 WARNING on \1 vs $1 |
810 | |
5a964f20 |
811 | Some people get too used to writing things like: |
cb1a09d0 |
812 | |
813 | $pattern =~ s/(\W)/\\\1/g; |
814 | |
815 | This is grandfathered for the RHS of a substitute to avoid shocking the |
816 | B<sed> addicts, but it's a dirty habit to get into. That's because in |
5f05dabc |
817 | PerlThink, the righthand side of a C<s///> is a double-quoted string. C<\1> in |
cb1a09d0 |
818 | the usual double-quoted string means a control-A. The customary Unix |
819 | meaning of C<\1> is kludged in for C<s///>. However, if you get into the habit |
820 | of doing that, you get yourself into trouble if you then add an C</e> |
821 | modifier. |
822 | |
5a964f20 |
823 | s/(\d+)/ \1 + 1 /eg; # causes warning under -w |
cb1a09d0 |
824 | |
825 | Or if you try to do |
826 | |
827 | s/(\d+)/\1000/; |
828 | |
829 | You can't disambiguate that by saying C<\{1}000>, whereas you can fix it with |
830 | C<${1}000>. Basically, the operation of interpolation should not be confused |
831 | with the operation of matching a backreference. Certainly they mean two |
832 | different things on the I<left> side of the C<s///>. |
9fa51da4 |
833 | |
c84d73f1 |
834 | =head2 Repeated patterns matching zero-length substring |
835 | |
836 | WARNING: Difficult material (and prose) ahead. This section needs a rewrite. |
837 | |
838 | Regular expressions provide a terse and powerful programming language. As |
839 | with most other power tools, power comes together with the ability |
840 | to wreak havoc. |
841 | |
842 | A common abuse of this power stems from the ability to make infinite |
843 | loops using regular expressions, with something as innocous as: |
844 | |
845 | 'foo' =~ m{ ( o? )* }x; |
846 | |
847 | The C<o?> can match at the beginning of C<'foo'>, and since the position |
848 | in the string is not moved by the match, C<o?> would match again and again |
849 | due to the C<*> modifier. Another common way to create a similar cycle |
850 | is with the looping modifier C<//g>: |
851 | |
852 | @matches = ( 'foo' =~ m{ o? }xg ); |
853 | |
854 | or |
855 | |
856 | print "match: <$&>\n" while 'foo' =~ m{ o? }xg; |
857 | |
858 | or the loop implied by split(). |
859 | |
860 | However, long experience has shown that many programming tasks may |
861 | be significantly simplified by using repeated subexpressions which |
862 | may match zero-length substrings, with a simple example being: |
863 | |
864 | @chars = split //, $string; # // is not magic in split |
865 | ($whitewashed = $string) =~ s/()/ /g; # parens avoid magic s// / |
866 | |
867 | Thus Perl allows the C</()/> construct, which I<forcefully breaks |
868 | the infinite loop>. The rules for this are different for lower-level |
869 | loops given by the greedy modifiers C<*+{}>, and for higher-level |
870 | ones like the C</g> modifier or split() operator. |
871 | |
872 | The lower-level loops are I<interrupted> when it is detected that a |
873 | repeated expression did match a zero-length substring, thus |
874 | |
875 | m{ (?: NON_ZERO_LENGTH | ZERO_LENGTH )* }x; |
876 | |
877 | is made equivalent to |
878 | |
879 | m{ (?: NON_ZERO_LENGTH )* |
880 | | |
881 | (?: ZERO_LENGTH )? |
882 | }x; |
883 | |
884 | The higher level-loops preserve an additional state between iterations: |
885 | whether the last match was zero-length. To break the loop, the following |
886 | match after a zero-length match is prohibited to have a length of zero. |
887 | This prohibition interacts with backtracking (see L<"Backtracking">), |
888 | and so the I<second best> match is chosen if the I<best> match is of |
889 | zero length. |
890 | |
891 | Say, |
892 | |
893 | $_ = 'bar'; |
894 | s/\w??/<$&>/g; |
895 | |
896 | results in C<"<><b><><a><><r><>">. At each position of the string the best |
897 | match given by non-greedy C<??> is the zero-length match, and the I<second |
898 | best> match is what is matched by C<\w>. Thus zero-length matches |
899 | alternate with one-character-long matches. |
900 | |
901 | Similarly, for repeated C<m/()/g> the second-best match is the match at the |
902 | position one notch further in the string. |
903 | |
904 | The additional state of being I<matched with zero-length> is associated to |
905 | the matched string, and is reset by each assignment to pos(). |
906 | |
907 | =head2 Creating custom RE engines |
908 | |
909 | Overloaded constants (see L<overload>) provide a simple way to extend |
910 | the functionality of the RE engine. |
911 | |
912 | Suppose that we want to enable a new RE escape-sequence C<\Y|> which |
913 | matches at boundary between white-space characters and non-whitespace |
914 | characters. Note that C<(?=\S)(?<!\S)|(?!\S)(?<=\S)> matches exactly |
915 | at these positions, so we want to have each C<\Y|> in the place of the |
916 | more complicated version. We can create a module C<customre> to do |
917 | this: |
918 | |
919 | package customre; |
920 | use overload; |
921 | |
922 | sub import { |
923 | shift; |
924 | die "No argument to customre::import allowed" if @_; |
925 | overload::constant 'qr' => \&convert; |
926 | } |
927 | |
928 | sub invalid { die "/$_[0]/: invalid escape '\\$_[1]'"} |
929 | |
930 | my %rules = ( '\\' => '\\', |
931 | 'Y|' => qr/(?=\S)(?<!\S)|(?!\S)(?<=\S)/ ); |
932 | sub convert { |
933 | my $re = shift; |
934 | $re =~ s{ |
935 | \\ ( \\ | Y . ) |
936 | } |
937 | { $rules{$1} or invalid($re,$1) }sgex; |
938 | return $re; |
939 | } |
940 | |
941 | Now C<use customre> enables the new escape in constant regular |
942 | expressions, i.e., those without any runtime variable interpolations. |
943 | As documented in L<overload>, this conversion will work only over |
944 | literal parts of regular expressions. For C<\Y|$re\Y|> the variable |
945 | part of this regular expression needs to be converted explicitly |
946 | (but only if the special meaning of C<\Y|> should be enabled inside $re): |
947 | |
948 | use customre; |
949 | $re = <>; |
950 | chomp $re; |
951 | $re = customre::convert $re; |
952 | /\Y|$re\Y|/; |
953 | |
9fa51da4 |
954 | =head2 SEE ALSO |
955 | |
9b599b2a |
956 | L<perlop/"Regexp Quote-Like Operators">. |
957 | |
1e66bd83 |
958 | L<perlop/"Gory details of parsing quoted constructs">. |
959 | |
9b599b2a |
960 | L<perlfunc/pos>. |
961 | |
962 | L<perllocale>. |
963 | |
5a964f20 |
964 | I<Mastering Regular Expressions> (see L<perlbook>) by Jeffrey Friedl. |