3 perlreref - Perl Regular Expressions Reference
7 This is a quick reference to Perl's regular expressions.
8 For full information see L<perlre> and L<perlop>, as well
9 as the L</"SEE ALSO"> section in this document.
13 C<=~> determines to which variable the regex is applied.
14 In its absence, $_ is used.
18 C<!~> determines to which variable the regex is applied,
19 and negates the result of the match; it returns
20 false if the match succeeds, and true if it fails.
24 C<m/pattern/msixpogc> searches a string for a pattern match,
25 applying the given options.
27 m Multiline mode - ^ and $ match internal lines
28 s match as a Single line - . matches \n
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
37 If 'pattern' is an empty string, the last I<successfully> matched
38 regex is used. Delimiters other than '/' may be used for both this
39 operator and the following ones. The leading C<m> can be ommitted
40 if the delimiter is '/'.
42 C<qr/pattern/msixpo> lets you store a regex in a variable,
43 or pass one around. Modifiers as for C<m//>, and are stored
46 C<s/pattern/replacement/msixpogce> substitutes matches of
47 'pattern' with 'replacement'. Modifiers as for C<m//>,
50 e Evaluate 'replacement' as an expression
52 'e' may be specified multiple times. 'replacement' is interpreted
53 as a double quoted string unless a single-quote (C<'>) is the delimiter.
55 C<?pattern?> is like C<m/pattern/> but matches only once. No alternate
56 delimiters can be used. Must be reset with reset().
60 \ Escapes the character immediately following it
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)
71 | Matches either the subexpression preceding or following it
72 \1, \2 ... Matches the text from the Nth group
74 =head2 ESCAPE SEQUENCES
76 These work as in normal strings.
84 \037 Any octal ASCII value
85 \x7f Any hexadecimal ASCII value
86 \x{263a} A wide hexadecimal value
88 \N{name} A named character
90 \l Lowercase next character
91 \u Titlecase next character
94 \Q Disable pattern metacharacters until \E
97 For Titlecase, see L</Titlecase>.
99 This one works differently from normal strings:
101 \b An assertion, not backspace, except in a character class
103 =head2 CHARACTER CLASSES
105 [amy] Match 'a', 'm' or 'y'
106 [f-j] Dash specifies "range"
107 [f-j-] Dash escaped or at start or end means 'dash'
108 [^f-j] Caret indicates "match any character _except_ these"
110 The following sequences work within or without a character class.
111 The first six are locale aware, all are Unicode aware. See L<perllocale>
112 and L<perlunicode> for details.
117 \W A non-word character
118 \s A whitespace character
119 \S A non-whitespace character
120 \h An horizontal white space
121 \H A non horizontal white space
122 \v A vertical white space
123 \V A non vertical white space
124 \R A generic newline (?>\v|\x0D\x0A)
126 \C Match a byte (with Unicode, '.' matches a character)
127 \pP Match P-named (Unicode) property
128 \p{...} Match Unicode property with long name
130 \P{...} Match lack of Unicode property with long name
131 \X Match extended Unicode combining character sequence
133 POSIX character classes and their Unicode and Perl equivalents:
135 alnum IsAlnum Alphanumeric
136 alpha IsAlpha Alphabetic
137 ascii IsASCII Any ASCII char
138 blank IsSpace [ \t] Horizontal whitespace (GNU extension)
139 cntrl IsCntrl Control characters
140 digit IsDigit \d Digits
141 graph IsGraph Alphanumeric and punctuation
142 lower IsLower Lowercase chars (locale and Unicode aware)
143 print IsPrint Alphanumeric, punct, and space
144 punct IsPunct Punctuation
145 space IsSpace [\s\ck] Whitespace
146 IsSpacePerl \s Perl's whitespace definition
147 upper IsUpper Uppercase chars (locale and Unicode aware)
148 word IsWord \w Alphanumeric plus _ (Perl extension)
149 xdigit IsXDigit [0-9A-Fa-f] Hexadecimal digit
151 Within a character class:
153 POSIX traditional Unicode
154 [:digit:] \d \p{IsDigit}
155 [:^digit:] \D \P{IsDigit}
159 All are zero-width assertions.
161 ^ Match string start (or line, if /m is used)
162 $ Match string end (or line, if /m is used) or before newline
163 \b Match word boundary (between \w and \W)
164 \B Match except at word boundary (between \w and \w or \W and \W)
165 \A Match string start (regardless of /m)
166 \Z Match string end (before optional newline)
167 \z Match absolute string end
168 \G Match where previous m//g left off
172 Quantifiers are greedy by default -- match the B<longest> leftmost.
174 Maximal Minimal Allowed range
175 ------- ------- -------------
176 {n,m} {n,m}? Must occur at least n times but no more than m times
177 {n,} {n,}? Must occur at least n times
178 {n} {n}? Must occur exactly n times
179 * *? 0 or more times (same as {0,})
180 + +? 1 or more times (same as {1,})
181 ? ?? 0 or 1 time (same as {0,1})
183 There is no quantifier {,n} -- that gets understood as a literal string.
185 =head2 EXTENDED CONSTRUCTS
188 (?imxs-imsx:...) Enable/disable option (as per m// modifiers)
189 (?=...) Zero-width positive lookahead assertion
190 (?!...) Zero-width negative lookahead assertion
191 (?<=...) Zero-width positive lookbehind assertion
192 (?<!...) Zero-width negative lookbehind assertion
193 (?>...) Grab what we can, prohibit backtracking
194 (?{ code }) Embedded code, return value becomes $^R
195 (??{ code }) Dynamic regex, return value used as regex
196 (?(cond)yes|no) cond being integer corresponding to capturing parens
197 (?(cond)yes) or a lookaround/eval zero-width assertion
201 $_ Default variable for operators to use
203 $` Everything prior to matched string
204 $& Entire matched string
205 $' Everything after to matched string
207 ${^PREMATCH} Everything prior to matched string
208 ${^MATCH} Entire matched string
209 ${^POSTMATCH} Everything after to matched string
211 The use of C<$`>, C<$&> or C<$'> will slow down B<all> regex use
212 within your program. Consult L<perlvar> for C<@LAST_MATCH_START>
213 to see equivalent expressions that won't cause slow down.
214 See also L<Devel::SawAmpersand>. Starting with Perl 5.10, you
215 can also use the equivalent variables C<${^PREMATCH}>, C<${^MATCH}>
216 and C<${^POSTMATCH}>, but for them to be defined, you have to
217 specify the C</p> (preserve) modifier on your regular expression.
219 $1, $2 ... hold the Xth captured expr
220 $+ Last parenthesized pattern match
221 $^N Holds the most recently closed capture
222 $^R Holds the result of the last (?{...}) expr
223 @- Offsets of starts of groups. $-[0] holds start of whole match
224 @+ Offsets of ends of groups. $+[0] holds end of whole match
225 %+ Named capture buffers
226 %- Named capture buffers, as array refs
228 Captured groups are numbered according to their I<opening> paren.
232 lc Lowercase a string
233 lcfirst Lowercase first char of a string
234 uc Uppercase a string
235 ucfirst Titlecase first char of a string
237 pos Return or set current match position
238 quotemeta Quote metacharacters
239 reset Reset ?pattern? status
240 study Analyze string for optimizing matching
242 split Use a regex to split a string into parts
244 The first four of these are like the escape sequences C<\L>, C<\l>,
245 C<\U>, and C<\u>. For Titlecase, see L</Titlecase>.
251 Unicode concept which most often is equal to uppercase, but for
252 certain characters like the German "sharp s" there is a difference.
258 This document may be distributed under the same terms as Perl itself.
266 L<perlretut> for a tutorial on regular expressions.
270 L<perlrequick> for a rapid tutorial.
274 L<perlre> for more details.
278 L<perlvar> for details on the variables.
282 L<perlop> for details on the operators.
286 L<perlfunc> for details on the functions.
290 L<perlfaq6> for FAQs on regular expressions.
294 The L<re> module to alter behaviour and aid
299 L<perldebug/"Debugging regular expressions">
303 L<perluniintro>, L<perlunicode>, L<charnames> and L<perllocale>
304 for details on regexes and internationalisation.
308 I<Mastering Regular Expressions> by Jeffrey Friedl
309 (F<http://regex.info/>) for a thorough grounding and
310 reference on the topic.