This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
utf16_to_utf8_reversed() should croak early when passed an odd byte length.
[perl5.git] / pod / perlreref.pod
CommitLineData
30487ceb
RGS
1=head1 NAME
2
3perlreref - Perl Regular Expressions Reference
4
5=head1 DESCRIPTION
6
7This is a quick reference to Perl's regular expressions.
8For full information see L<perlre> and L<perlop>, as well
6d014f17 9as the L</"SEE ALSO"> section in this document.
30487ceb 10
a5365663 11=head2 OPERATORS
30487ceb 12
e17472c5
RGS
13C<=~> determines to which variable the regex is applied.
14In its absence, $_ is used.
30487ceb 15
e17472c5 16 $var =~ /foo/;
30487ceb 17
e17472c5
RGS
18C<!~> determines to which variable the regex is applied,
19and negates the result of the match; it returns
20false if the match succeeds, and true if it fails.
6d014f17 21
e17472c5 22 $var !~ /foo/;
6d014f17 23
e17472c5
RGS
24C<m/pattern/msixpogc> searches a string for a pattern match,
25applying the given options.
30487ceb 26
e17472c5
RGS
27 m Multiline mode - ^ and $ match internal lines
28 s match as a Single line - . matches \n
29 i case-Insensitive
30 x eXtended legibility - free whitespace and comments
31 p Preserve a copy of the matched string -
32 ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH} will be defined.
33 o compile pattern Once
34 g Global - all occurrences
35 c don't reset pos on failed matches when using /g
30487ceb 36
e17472c5
RGS
37If 'pattern' is an empty string, the last I<successfully> matched
38regex is used. Delimiters other than '/' may be used for both this
64c5a566 39operator and the following ones. The leading C<m> can be omitted
e17472c5 40if the delimiter is '/'.
30487ceb 41
e17472c5
RGS
42C<qr/pattern/msixpo> lets you store a regex in a variable,
43or pass one around. Modifiers as for C<m//>, and are stored
44within the regex.
30487ceb 45
e17472c5
RGS
46C<s/pattern/replacement/msixpogce> substitutes matches of
47'pattern' with 'replacement'. Modifiers as for C<m//>,
48with one addition:
30487ceb 49
e17472c5 50 e Evaluate 'replacement' as an expression
30487ceb 51
e17472c5
RGS
52'e' may be specified multiple times. 'replacement' is interpreted
53as a double quoted string unless a single-quote (C<'>) is the delimiter.
30487ceb 54
e17472c5
RGS
55C<?pattern?> is like C<m/pattern/> but matches only once. No alternate
56delimiters can be used. Must be reset with reset().
30487ceb 57
a5365663 58=head2 SYNTAX
30487ceb 59
6d014f17 60 \ Escapes the character immediately following it
e5a7b003
RGS
61 . Matches any single character except a newline (unless /s is used)
62 ^ Matches at the beginning of the string (or line, if /m is used)
63 $ Matches at the end of the string (or line, if /m is used)
64 * Matches the preceding element 0 or more times
65 + Matches the preceding element 1 or more times
66 ? Matches the preceding element 0 or 1 times
67 {...} Specifies a range of occurrences for the element preceding it
68 [...] Matches any one of the characters contained within the brackets
69 (...) Groups subexpressions for capturing to $1, $2...
70 (?:...) Groups subexpressions without capturing (cluster)
6d014f17 71 | Matches either the subexpression preceding or following it
64c5a566
RGS
72 \1, \2, \3 ... Matches the text from the Nth group
73 \g1 or \g{1}, \g2 ... Matches the text from the Nth group
74 \g-1 or \g{-1}, \g-2 ... Matches the text from the Nth previous group
75 \g{name} Named backreference
76 \k<name> Named backreference
77 \k'name' Named backreference
78 (?P=name) Named backreference (python syntax)
30487ceb
RGS
79
80=head2 ESCAPE SEQUENCES
81
82These work as in normal strings.
83
84 \a Alarm (beep)
85 \e Escape
86 \f Formfeed
87 \n Newline
88 \r Carriage return
89 \t Tab
6ed007ae 90 \037 Any octal ASCII value
30487ceb
RGS
91 \x7f Any hexadecimal ASCII value
92 \x{263a} A wide hexadecimal value
93 \cx Control-x
94 \N{name} A named character
95
6d014f17 96 \l Lowercase next character
d3b55b48 97 \u Titlecase next character
30487ceb 98 \L Lowercase until \E
d3b55b48 99 \U Uppercase until \E
30487ceb 100 \Q Disable pattern metacharacters until \E
e17472c5 101 \E End modification
30487ceb 102
47e8a552
IT
103For Titlecase, see L</Titlecase>.
104
30487ceb
RGS
105This one works differently from normal strings:
106
107 \b An assertion, not backspace, except in a character class
108
109=head2 CHARACTER CLASSES
110
111 [amy] Match 'a', 'm' or 'y'
112 [f-j] Dash specifies "range"
113 [f-j-] Dash escaped or at start or end means 'dash'
6d014f17 114 [^f-j] Caret indicates "match any character _except_ these"
30487ceb 115
e04a154e 116The following sequences work within or without a character class.
e17472c5
RGS
117The first six are locale aware, all are Unicode aware. See L<perllocale>
118and L<perlunicode> for details.
119
120 \d A digit
121 \D A nondigit
122 \w A word character
123 \W A non-word character
124 \s A whitespace character
125 \S A non-whitespace character
126 \h An horizontal white space
127 \H A non horizontal white space
c741660a 128 \N A non newline (like . without /s)
e17472c5
RGS
129 \v A vertical white space
130 \V A non vertical white space
131 \R A generic newline (?>\v|\x0D\x0A)
e04a154e
JH
132
133 \C Match a byte (with Unicode, '.' matches a character)
30487ceb
RGS
134 \pP Match P-named (Unicode) property
135 \p{...} Match Unicode property with long name
136 \PP Match non-P
137 \P{...} Match lack of Unicode property with long name
e17472c5 138 \X Match extended Unicode combining character sequence
30487ceb
RGS
139
140POSIX character classes and their Unicode and Perl equivalents:
141
e04a154e
JH
142 alnum IsAlnum Alphanumeric
143 alpha IsAlpha Alphabetic
144 ascii IsASCII Any ASCII char
145 blank IsSpace [ \t] Horizontal whitespace (GNU extension)
146 cntrl IsCntrl Control characters
147 digit IsDigit \d Digits
148 graph IsGraph Alphanumeric and punctuation
149 lower IsLower Lowercase chars (locale and Unicode aware)
150 print IsPrint Alphanumeric, punct, and space
151 punct IsPunct Punctuation
152 space IsSpace [\s\ck] Whitespace
153 IsSpacePerl \s Perl's whitespace definition
154 upper IsUpper Uppercase chars (locale and Unicode aware)
155 word IsWord \w Alphanumeric plus _ (Perl extension)
156 xdigit IsXDigit [0-9A-Fa-f] Hexadecimal digit
30487ceb
RGS
157
158Within a character class:
159
160 POSIX traditional Unicode
161 [:digit:] \d \p{IsDigit}
162 [:^digit:] \D \P{IsDigit}
163
164=head2 ANCHORS
165
166All are zero-width assertions.
167
168 ^ Match string start (or line, if /m is used)
169 $ Match string end (or line, if /m is used) or before newline
170 \b Match word boundary (between \w and \W)
6d014f17 171 \B Match except at word boundary (between \w and \w or \W and \W)
30487ceb 172 \A Match string start (regardless of /m)
6d014f17 173 \Z Match string end (before optional newline)
30487ceb
RGS
174 \z Match absolute string end
175 \G Match where previous m//g left off
30487ceb 176
64c5a566
RGS
177 \K Keep the stuff left of the \K, don't include it in $&
178
30487ceb
RGS
179=head2 QUANTIFIERS
180
6d014f17 181Quantifiers are greedy by default -- match the B<longest> leftmost.
30487ceb 182
64c5a566
RGS
183 Maximal Minimal Possessive Allowed range
184 ------- ------- ---------- -------------
185 {n,m} {n,m}? {n,m}+ Must occur at least n times
186 but no more than m times
187 {n,} {n,}? {n,}+ Must occur at least n times
188 {n} {n}? {n}+ Must occur exactly n times
189 * *? *+ 0 or more times (same as {0,})
190 + +? ++ 1 or more times (same as {1,})
191 ? ?? ?+ 0 or 1 time (same as {0,1})
192
193The possessive forms (new in Perl 5.10) prevent backtracking: what gets
194matched by a pattern with a possessive quantifier will not be backtracked
195into, even if that causes the whole match to fail.
30487ceb 196
6d014f17
JH
197There is no quantifier {,n} -- that gets understood as a literal string.
198
30487ceb
RGS
199=head2 EXTENDED CONSTRUCTS
200
64c5a566
RGS
201 (?#text) A comment
202 (?:...) Groups subexpressions without capturing (cluster)
203 (?pimsx-imsx:...) Enable/disable option (as per m// modifiers)
204 (?=...) Zero-width positive lookahead assertion
205 (?!...) Zero-width negative lookahead assertion
206 (?<=...) Zero-width positive lookbehind assertion
207 (?<!...) Zero-width negative lookbehind assertion
208 (?>...) Grab what we can, prohibit backtracking
209 (?|...) Branch reset
210 (?<name>...) Named capture
211 (?'name'...) Named capture
212 (?P<name>...) Named capture (python syntax)
213 (?{ code }) Embedded code, return value becomes $^R
214 (??{ code }) Dynamic regex, return value used as regex
215 (?N) Recurse into subpattern number N
216 (?-N), (?+N) Recurse into Nth previous/next subpattern
217 (?R), (?0) Recurse at the beginning of the whole pattern
218 (?&name) Recurse into a named subpattern
219 (?P>name) Recurse into a named subpattern (python syntax)
220 (?(cond)yes|no)
221 (?(cond)yes) Conditional expression, where "cond" can be:
222 (N) subpattern N has matched something
223 (<name>) named subpattern has matched something
224 ('name') named subpattern has matched something
225 (?{code}) code condition
226 (R) true if recursing
227 (RN) true if recursing into Nth subpattern
228 (R&name) true if recursing into named subpattern
229 (DEFINE) always false, no no-pattern allowed
30487ceb 230
a5365663 231=head2 VARIABLES
30487ceb
RGS
232
233 $_ Default variable for operators to use
30487ceb 234
30487ceb 235 $` Everything prior to matched string
e17472c5 236 $& Entire matched string
30487ceb
RGS
237 $' Everything after to matched string
238
e17472c5
RGS
239 ${^PREMATCH} Everything prior to matched string
240 ${^MATCH} Entire matched string
241 ${^POSTMATCH} Everything after to matched string
242
243The use of C<$`>, C<$&> or C<$'> will slow down B<all> regex use
64c5a566 244within your program. Consult L<perlvar> for C<@->
30487ceb 245to see equivalent expressions that won't cause slow down.
e17472c5
RGS
246See also L<Devel::SawAmpersand>. Starting with Perl 5.10, you
247can also use the equivalent variables C<${^PREMATCH}>, C<${^MATCH}>
248and C<${^POSTMATCH}>, but for them to be defined, you have to
249specify the C</p> (preserve) modifier on your regular expression.
30487ceb
RGS
250
251 $1, $2 ... hold the Xth captured expr
252 $+ Last parenthesized pattern match
253 $^N Holds the most recently closed capture
254 $^R Holds the result of the last (?{...}) expr
6d014f17
JH
255 @- Offsets of starts of groups. $-[0] holds start of whole match
256 @+ Offsets of ends of groups. $+[0] holds end of whole match
e17472c5
RGS
257 %+ Named capture buffers
258 %- Named capture buffers, as array refs
30487ceb 259
6d014f17 260Captured groups are numbered according to their I<opening> paren.
30487ceb 261
a5365663 262=head2 FUNCTIONS
30487ceb
RGS
263
264 lc Lowercase a string
265 lcfirst Lowercase first char of a string
266 uc Uppercase a string
47e8a552
IT
267 ucfirst Titlecase first char of a string
268
30487ceb
RGS
269 pos Return or set current match position
270 quotemeta Quote metacharacters
271 reset Reset ?pattern? status
272 study Analyze string for optimizing matching
273
e17472c5 274 split Use a regex to split a string into parts
30487ceb 275
d3b55b48
JH
276The first four of these are like the escape sequences C<\L>, C<\l>,
277C<\U>, and C<\u>. For Titlecase, see L</Titlecase>.
47e8a552 278
1501d360 279=head2 TERMINOLOGY
47e8a552 280
a5365663 281=head3 Titlecase
47e8a552
IT
282
283Unicode concept which most often is equal to uppercase, but for
284certain characters like the German "sharp s" there is a difference.
285
40506b5d 286=head1 AUTHOR
30487ceb 287
64c5a566 288Iain Truskett. Updated by the Perl 5 Porters.
30487ceb
RGS
289
290This document may be distributed under the same terms as Perl itself.
291
40506b5d 292=head1 SEE ALSO
30487ceb
RGS
293
294=over 4
295
296=item *
297
298L<perlretut> for a tutorial on regular expressions.
299
300=item *
301
302L<perlrequick> for a rapid tutorial.
303
304=item *
305
306L<perlre> for more details.
307
308=item *
309
310L<perlvar> for details on the variables.
311
312=item *
313
314L<perlop> for details on the operators.
315
316=item *
317
318L<perlfunc> for details on the functions.
319
320=item *
321
322L<perlfaq6> for FAQs on regular expressions.
323
324=item *
325
64c5a566
RGS
326L<perlrebackslash> for a reference on backslash sequences.
327
328=item *
329
330L<perlrecharclass> for a reference on character classes.
331
332=item *
333
30487ceb
RGS
334The L<re> module to alter behaviour and aid
335debugging.
336
337=item *
338
339L<perldebug/"Debugging regular expressions">
340
341=item *
342
e17472c5 343L<perluniintro>, L<perlunicode>, L<charnames> and L<perllocale>
30487ceb
RGS
344for details on regexes and internationalisation.
345
346=item *
347
348I<Mastering Regular Expressions> by Jeffrey Friedl
08d7a6b2 349(F<http://oreilly.com/catalog/9780596528126/>) for a thorough grounding and
30487ceb
RGS
350reference on the topic.
351
352=back
353
40506b5d 354=head1 THANKS
30487ceb
RGS
355
356David P.C. Wollmann,
357Richard Soderberg,
358Sean M. Burke,
359Tom Christiansen,
e5a7b003 360Jim Cromie,
30487ceb
RGS
361and
362Jeffrey Goff
363for useful advice.
6d014f17
JH
364
365=cut