This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perldelta for 2384afee9 / #123553
[perl5.git] / pod / perlrecharclass.pod
CommitLineData
8a118206 1=head1 NAME
ea449505 2X<character class>
8a118206
RGS
3
4perlrecharclass - Perl Regular Expression Character Classes
5
6=head1 DESCRIPTION
7
8The top level documentation about Perl regular expressions
9is found in L<perlre>.
10
11This manual page discusses the syntax and use of character
6b83a163 12classes in Perl regular expressions.
8a118206 13
6b83a163 14A character class is a way of denoting a set of characters
8a118206 15in such a way that one character of the set is matched.
6b83a163 16It's important to remember that: matching a character class
8a118206
RGS
17consumes exactly one character in the source string. (The source
18string is the string the regular expression is matched against.)
19
20There are three types of character classes in Perl regular
6b83a163 21expressions: the dot, backslash sequences, and the form enclosed in square
ea449505 22brackets. Keep in mind, though, that often the term "character class" is used
6b83a163 23to mean just the bracketed form. Certainly, most Perl documentation does that.
8a118206
RGS
24
25=head2 The dot
26
27The dot (or period), C<.> is probably the most used, and certainly
28the most well-known character class. By default, a dot matches any
5db9882c 29character, except for the newline. That default can be changed to
6b83a163
KW
30add matching the newline by using the I<single line> modifier: either
31for the entire regular expression with the C</s> modifier, or
d66e1f56 32locally with C<(?s)>. (The C<L</\N>> backslash sequence, described
6b83a163
KW
33below, matches any character except newline without regard to the
34I<single line> modifier.)
8a118206
RGS
35
36Here are some examples:
37
38 "a" =~ /./ # Match
39 "." =~ /./ # Match
40 "" =~ /./ # No match (dot has to match a character)
41 "\n" =~ /./ # No match (dot does not match a newline)
42 "\n" =~ /./s # Match (global 'single line' modifier)
43 "\n" =~ /(?s:.)/ # Match (local 'single line' modifier)
44 "ab" =~ /^.$/ # No match (dot matches one character)
45
6b83a163 46=head2 Backslash sequences
82206b5e 47X<\w> X<\W> X<\s> X<\S> X<\d> X<\D> X<\p> X<\P>
ea449505
KW
48X<\N> X<\v> X<\V> X<\h> X<\H>
49X<word> X<whitespace>
8a118206 50
6b83a163
KW
51A backslash sequence is a sequence of characters, the first one of which is a
52backslash. Perl ascribes special meaning to many such sequences, and some of
53these are character classes. That is, they match a single character each,
54provided that the character belongs to the specific set of characters defined
55by the sequence.
8a118206 56
6b83a163
KW
57Here's a list of the backslash sequences that are character classes. They
58are discussed in more detail below. (For the backslash sequences that aren't
59character classes, see L<perlrebackslash>.)
8a118206 60
6b83a163
KW
61 \d Match a decimal digit character.
62 \D Match a non-decimal-digit character.
8a118206
RGS
63 \w Match a "word" character.
64 \W Match a non-"word" character.
ea449505
KW
65 \s Match a whitespace character.
66 \S Match a non-whitespace character.
67 \h Match a horizontal whitespace character.
68 \H Match a character that isn't horizontal whitespace.
ea449505
KW
69 \v Match a vertical whitespace character.
70 \V Match a character that isn't vertical whitespace.
4e5e0888 71 \N Match a character that isn't a newline.
6b83a163 72 \pP, \p{Prop} Match a character that has the given Unicode property.
6c5a041f 73 \PP, \P{Prop} Match a character that doesn't have the Unicode property
8a118206 74
1433f837
KW
75=head3 \N
76
2171640d 77C<\N>, available starting in v5.12, like the dot, matches any
1433f837
KW
78character that is not a newline. The difference is that C<\N> is not influenced
79by the I<single line> regular expression modifier (see L</The dot> above). Note
80that the form C<\N{...}> may mean something completely different. When the
81C<{...}> is a L<quantifier|perlre/Quantifiers>, it means to match a non-newline
82character that many times. For example, C<\N{3}> means to match 3
83non-newlines; C<\N{5,}> means to match 5 or more non-newlines. But if C<{...}>
84is not a legal quantifier, it is presumed to be a named character. See
85L<charnames> for those. For example, none of C<\N{COLON}>, C<\N{4F}>, and
86C<\N{F4}> contain legal quantifiers, so Perl will try to find characters whose
87names are respectively C<COLON>, C<4F>, and C<F4>.
88
8a118206
RGS
89=head3 Digits
90
b6538e4f 91C<\d> matches a single character considered to be a decimal I<digit>.
5db9882c 92If the C</a> regular expression modifier is in effect, it matches [0-9].
582da942 93Otherwise, it
82206b5e
KW
94matches anything that is matched by C<\p{Digit}>, which includes [0-9].
95(An unlikely possible exception is that under locale matching rules, the
d66e1f56
KW
96current locale might not have C<[0-9]> matched by C<\d>, and/or might match
97other characters whose code point is less than 256. The only such locale
98definitions that are legal would be to match C<[0-9]> plus another set of
9910 consecutive digit characters; anything else would be in violation of
100the C language standard, but Perl doesn't currently assume anything in
101regard to this.)
82206b5e
KW
102
103What this means is that unless the C</a> modifier is in effect C<\d> not
104only matches the digits '0' - '9', but also Arabic, Devanagari, and
105digits from other languages. This may cause some confusion, and some
106security issues.
107
108Some digits that C<\d> matches look like some of the [0-9] ones, but
109have different values. For example, BENGALI DIGIT FOUR (U+09EA) looks
110very much like an ASCII DIGIT EIGHT (U+0038). An application that
111is expecting only the ASCII digits might be misled, or if the match is
112C<\d+>, the matched string might contain a mixture of digits from
113different writing systems that look like they signify a number different
67592e11 114than they actually do. L<Unicode::UCD/num()> can
e397bccf 115be used to safely
82206b5e
KW
116calculate the value, returning C<undef> if the input string contains
117such a mixture.
118
119What C<\p{Digit}> means (and hence C<\d> except under the C</a>
120modifier) is C<\p{General_Category=Decimal_Number}>, or synonymously,
121C<\p{General_Category=Digit}>. Starting with Unicode version 4.1, this
122is the same set of characters matched by C<\p{Numeric_Type=Decimal}>.
6b83a163
KW
123But Unicode also has a different property with a similar name,
124C<\p{Numeric_Type=Digit}>, which matches a completely different set of
82206b5e
KW
125characters. These characters are things such as C<CIRCLED DIGIT ONE>
126or subscripts, or are from writing systems that lack all ten digits.
6b83a163 127
82206b5e
KW
128The design intent is for C<\d> to exactly match the set of characters
129that can safely be used with "normal" big-endian positional decimal
130syntax, where, for example 123 means one 'hundred', plus two 'tens',
131plus three 'ones'. This positional notation does not necessarily apply
132to characters that match the other type of "digit",
133C<\p{Numeric_Type=Digit}>, and so C<\d> doesn't match them.
6b83a163 134
e2cfb18c 135The Tamil digits (U+0BE6 - U+0BEF) can also legally be
82206b5e
KW
136used in old-style Tamil numbers in which they would appear no more than
137one in a row, separated by characters that mean "times 10", "times 100",
138etc. (See L<http://www.unicode.org/notes/tn21>.)
8a118206 139
b6538e4f 140Any character not matched by C<\d> is matched by C<\D>.
8a118206
RGS
141
142=head3 Word characters
143
ea449505 144A C<\w> matches a single alphanumeric character (an alphabetic character, or a
41805eb9
KW
145decimal digit); or a connecting punctuation character, such as an
146underscore ("_"); or a "mark" character (like some sort of accent) that
147attaches to one of those. It does not match a whole word. To match a
148whole word, use C<\w+>. This isn't the same thing as matching an
149English word, but in the ASCII range it is the same as a string of
150Perl-identifier characters.
82206b5e
KW
151
152=over
153
154=item If the C</a> modifier is in effect ...
155
156C<\w> matches the 63 characters [a-zA-Z0-9_].
157
158=item otherwise ...
159
160=over
161
162=item For code points above 255 ...
163
164C<\w> matches the same as C<\p{Word}> matches in this range. That is,
165it matches Thai letters, Greek letters, etc. This includes connector
d35dd6c6 166punctuation (like the underscore) which connect two words together, or
b6538e4f 167diacritics, such as a C<COMBINING TILDE> and the modifier letters, which
82206b5e
KW
168are generally used to add auxiliary markings to letters.
169
170=item For code points below 256 ...
171
172=over
173
174=item if locale rules are in effect ...
175
176C<\w> matches the platform's native underscore character plus whatever
177the locale considers to be alphanumeric.
178
4b9734bf 179=item if Unicode rules are in effect ...
82206b5e
KW
180
181C<\w> matches exactly what C<\p{Word}> matches.
182
183=item otherwise ...
184
185C<\w> matches [a-zA-Z0-9_].
186
187=back
188
189=back
190
191=back
192
193Which rules apply are determined as described in L<perlre/Which character set modifier is in effect?>.
8a118206 194
6b83a163
KW
195There are a number of security issues with the full Unicode list of word
196characters. See L<http://unicode.org/reports/tr36>.
197
198Also, for a somewhat finer-grained set of characters that are in programming
199language identifiers beyond the ASCII range, you may wish to instead use the
e2cfb18c
KW
200more customized L</Unicode Properties>, C<\p{ID_Start}>,
201C<\p{ID_Continue}>, C<\p{XID_Start}>, and C<\p{XID_Continue}>. See
202L<http://unicode.org/reports/tr31>.
6b83a163 203
b6538e4f 204Any character not matched by C<\w> is matched by C<\W>.
8a118206 205
ea449505
KW
206=head3 Whitespace
207
82206b5e
KW
208C<\s> matches any single character considered whitespace.
209
210=over
211
212=item If the C</a> modifier is in effect ...
213
d28d8023
KW
214In all Perl versions, C<\s> matches the 5 characters [\t\n\f\r ]; that
215is, the horizontal tab,
216the newline, the form feed, the carriage return, and the space.
217Starting in Perl v5.18, experimentally, it also matches the vertical tab, C<\cK>.
218See note C<[1]> below for a discussion of this.
82206b5e
KW
219
220=item otherwise ...
221
222=over
223
224=item For code points above 255 ...
225
226C<\s> matches exactly the code points above 255 shown with an "s" column
227in the table below.
228
229=item For code points below 256 ...
230
231=over
232
233=item if locale rules are in effect ...
234
d28d8023 235C<\s> matches whatever the locale considers to be whitespace.
82206b5e 236
4b9734bf 237=item if Unicode rules are in effect ...
82206b5e
KW
238
239C<\s> matches exactly the characters shown with an "s" column in the
240table below.
241
242=item otherwise ...
243
2941e8b2 244C<\s> matches [\t\n\f\r ] and, starting, experimentally in Perl
d28d8023
KW
245v5.18, the vertical tab, C<\cK>.
246(See note C<[1]> below for a discussion of this.)
82206b5e
KW
247Note that this list doesn't include the non-breaking space.
248
249=back
250
251=back
252
253=back
254
255Which rules apply are determined as described in L<perlre/Which character set modifier is in effect?>.
8a118206 256
b6538e4f 257Any character not matched by C<\s> is matched by C<\S>.
8a118206 258
b6538e4f 259C<\h> matches any character considered horizontal whitespace;
8129baca 260this includes the platform's space and tab characters and several others
b6538e4f 261listed in the table below. C<\H> matches any character
8129baca
KW
262not considered horizontal whitespace. They use the platform's native
263character set, and do not consider any locale that may otherwise be in
264use.
ea449505 265
b6538e4f 266C<\v> matches any character considered vertical whitespace;
8129baca 267this includes the platform's carriage return and line feed characters (newline)
b6538e4f
TC
268plus several other characters, all listed in the table below.
269C<\V> matches any character not considered vertical whitespace.
8129baca
KW
270They use the platform's native character set, and do not consider any
271locale that may otherwise be in use.
8a118206
RGS
272
273C<\R> matches anything that can be considered a newline under Unicode
274rules. It's not a character class, as it can match a multi-character
275sequence. Therefore, it cannot be used inside a bracketed character
8129baca
KW
276class; use C<\v> instead (vertical whitespace). It uses the platform's
277native character set, and does not consider any locale that may
278otherwise be in use.
ea449505 279Details are discussed in L<perlrebackslash>.
8a118206 280
82206b5e 281Note that unlike C<\s> (and C<\d> and C<\w>), C<\h> and C<\v> always match
8129baca
KW
282the same characters, without regard to other factors, such as the active
283locale or whether the source string is in UTF-8 format.
8a118206 284
d28d8023
KW
285One might think that C<\s> is equivalent to C<[\h\v]>. This is indeed true
286starting in Perl v5.18, but prior to that, the sole difference was that the
287vertical tab (C<"\cK">) was not matched by C<\s>.
8a118206
RGS
288
289The following table is a complete listing of characters matched by
a9c9e371 290C<\s>, C<\h> and C<\v> as of Unicode 6.3.
8a118206 291
582da942 292The first column gives the Unicode code point of the character (in hex format),
8a118206 293the second column gives the (Unicode) name. The third column indicates
4b9734bf
KW
294by which class(es) the character is matched (assuming no locale is in
295effect that changes the C<\s> matching).
8a118206 296
fc28d2a3
KW
297 0x0009 CHARACTER TABULATION h s
298 0x000a LINE FEED (LF) vs
d28d8023 299 0x000b LINE TABULATION vs [1]
fc28d2a3
KW
300 0x000c FORM FEED (FF) vs
301 0x000d CARRIAGE RETURN (CR) vs
302 0x0020 SPACE h s
d28d8023
KW
303 0x0085 NEXT LINE (NEL) vs [2]
304 0x00a0 NO-BREAK SPACE h s [2]
fc28d2a3 305 0x1680 OGHAM SPACE MARK h s
fc28d2a3
KW
306 0x2000 EN QUAD h s
307 0x2001 EM QUAD h s
308 0x2002 EN SPACE h s
309 0x2003 EM SPACE h s
310 0x2004 THREE-PER-EM SPACE h s
311 0x2005 FOUR-PER-EM SPACE h s
312 0x2006 SIX-PER-EM SPACE h s
313 0x2007 FIGURE SPACE h s
314 0x2008 PUNCTUATION SPACE h s
315 0x2009 THIN SPACE h s
316 0x200a HAIR SPACE h s
317 0x2028 LINE SEPARATOR vs
318 0x2029 PARAGRAPH SEPARATOR vs
319 0x202f NARROW NO-BREAK SPACE h s
320 0x205f MEDIUM MATHEMATICAL SPACE h s
321 0x3000 IDEOGRAPHIC SPACE h s
8a118206
RGS
322
323=over 4
324
325=item [1]
326
d28d8023
KW
327Prior to Perl v5.18, C<\s> did not match the vertical tab. The change
328in v5.18 is considered an experiment, which means it could be backed out
a04e6aad 329in v5.22 if experience indicates that it breaks too much
d28d8023
KW
330existing code. If this change adversely affects you, send email to
331C<perlbug@perl.org>; if it affects you positively, email
332C<perlthanks@perl.org>. In the meantime, C<[^\S\cK]> (obscurely)
333matches what C<\s> traditionally did.
334
335=item [2]
336
82206b5e
KW
337NEXT LINE and NO-BREAK SPACE may or may not match C<\s> depending
338on the rules in effect. See
339L<the beginning of this section|/Whitespace>.
8a118206
RGS
340
341=back
342
8a118206
RGS
343=head3 Unicode Properties
344
c1c4ae3a
KW
345C<\pP> and C<\p{Prop}> are character classes to match characters that fit given
346Unicode properties. One letter property names can be used in the C<\pP> form,
347with the property name following the C<\p>, otherwise, braces are required.
348When using braces, there is a single form, which is just the property name
349enclosed in the braces, and a compound form which looks like C<\p{name=value}>,
b6538e4f 350which means to match if the property "name" for the character has that particular
c1c4ae3a 351"value".
e1b711da
KW
352For instance, a match for a number can be written as C</\pN/> or as
353C</\p{Number}/>, or as C</\p{Number=True}/>.
354Lowercase letters are matched by the property I<Lowercase_Letter> which
e2cfb18c 355has the short form I<Ll>. They need the braces, so are written as C</\p{Ll}/> or
e1b711da
KW
356C</\p{Lowercase_Letter}/>, or C</\p{General_Category=Lowercase_Letter}/>
357(the underscores are optional).
358C</\pLl/> is valid, but means something different.
8a118206
RGS
359It matches a two character string: a letter (Unicode property C<\pL>),
360followed by a lowercase C<l>.
361
bc943be5 362If locale rules are not in effect, the use of
82206b5e 363a Unicode property will force the regular expression into using Unicode
bc943be5 364rules, if it isn't already.
82206b5e 365
56ca34ca
KW
366Note that almost all properties are immune to case-insensitive matching.
367That is, adding a C</i> regular expression modifier does not change what
82206b5e 368they match. There are two sets that are affected. The first set is
56ca34ca
KW
369C<Uppercase_Letter>,
370C<Lowercase_Letter>,
371and C<Titlecase_Letter>,
372all of which match C<Cased_Letter> under C</i> matching.
b6538e4f 373The second set is
56ca34ca
KW
374C<Uppercase>,
375C<Lowercase>,
376and C<Titlecase>,
377all of which match C<Cased> under C</i> matching.
378(The difference between these sets is that some things, such as Roman
e2cfb18c 379numerals, come in both upper and lower case, so they are C<Cased>, but
b6538e4f 380aren't considered to be letters, so they aren't C<Cased_Letter>s. They're
82206b5e
KW
381actually C<Letter_Number>s.)
382This set also includes its subsets C<PosixUpper> and C<PosixLower>, both
e2cfb18c 383of which under C</i> match C<PosixAlpha>.
56ca34ca
KW
384
385For more details on Unicode properties, see L<perlunicode/Unicode
386Character Properties>; for a
e1b711da 387complete list of possible properties, see
56ca34ca
KW
388L<perluniprops/Properties accessible through \p{} and \P{}>,
389which notes all forms that have C</i> differences.
e1b711da 390It is also possible to define your own properties. This is discussed in
8a118206
RGS
391L<perlunicode/User-Defined Character Properties>.
392
94b42e47 393Unicode properties are defined (surprise!) only on Unicode code points.
2d88a86a
KW
394Starting in v5.20, when matching against C<\p> and C<\P>, Perl treats
395non-Unicode code points (those above the legal Unicode maximum of
3960x10FFFF) as if they were typical unassigned Unicode code points.
94b42e47 397
2d88a86a
KW
398Prior to v5.20, Perl raised a warning and made all matches fail on
399non-Unicode code points. This could be somewhat surprising:
94b42e47 400
2d88a86a
KW
401 chr(0x110000) =~ \p{ASCII_Hex_Digit=True} # Fails on Perls < v5.20.
402 chr(0x110000) =~ \p{ASCII_Hex_Digit=False} # Also fails on Perls
403 # < v5.20
404
405Even though these two matches might be thought of as complements, until
406v5.20 they were so only on Unicode code points.
94b42e47 407
8a118206
RGS
408=head4 Examples
409
410 "a" =~ /\w/ # Match, "a" is a 'word' character.
411 "7" =~ /\w/ # Match, "7" is a 'word' character as well.
412 "a" =~ /\d/ # No match, "a" isn't a digit.
413 "7" =~ /\d/ # Match, "7" is a digit.
ea449505 414 " " =~ /\s/ # Match, a space is whitespace.
8a118206
RGS
415 "a" =~ /\D/ # Match, "a" is a non-digit.
416 "7" =~ /\D/ # No match, "7" is not a non-digit.
ea449505 417 " " =~ /\S/ # No match, a space is not non-whitespace.
8a118206 418
ea449505
KW
419 " " =~ /\h/ # Match, space is horizontal whitespace.
420 " " =~ /\v/ # No match, space is not vertical whitespace.
421 "\r" =~ /\v/ # Match, a return is vertical whitespace.
8a118206
RGS
422
423 "a" =~ /\pL/ # Match, "a" is a letter.
424 "a" =~ /\p{Lu}/ # No match, /\p{Lu}/ matches upper case letters.
425
426 "\x{0e0b}" =~ /\p{Thai}/ # Match, \x{0e0b} is the character
427 # 'THAI CHARACTER SO SO', and that's in
428 # Thai Unicode class.
ea449505 429 "a" =~ /\P{Lao}/ # Match, as "a" is not a Laotian character.
8a118206 430
82206b5e
KW
431It is worth emphasizing that C<\d>, C<\w>, etc, match single characters, not
432complete numbers or words. To match a number (that consists of digits),
433use C<\d+>; to match a word, use C<\w+>. But be aware of the security
434considerations in doing so, as mentioned above.
8a118206
RGS
435
436=head2 Bracketed Character Classes
437
438The third form of character class you can use in Perl regular expressions
6b83a163 439is the bracketed character class. In its simplest form, it lists the characters
c1c4ae3a 440that may be matched, surrounded by square brackets, like this: C<[aeiou]>.
ea449505 441This matches one of C<a>, C<e>, C<i>, C<o> or C<u>. Like the other
1f59b283 442character classes, exactly one character is matched.* To match
ea449505 443a longer string consisting of characters mentioned in the character
6b83a163 444class, follow the character class with a L<quantifier|perlre/Quantifiers>. For
b6538e4f 445instance, C<[aeiou]+> matches one or more lowercase English vowels.
8a118206
RGS
446
447Repeating a character in a character class has no
448effect; it's considered to be in the set only once.
449
450Examples:
451
452 "e" =~ /[aeiou]/ # Match, as "e" is listed in the class.
453 "p" =~ /[aeiou]/ # No match, "p" is not listed in the class.
454 "ae" =~ /^[aeiou]$/ # No match, a character class only matches
455 # a single character.
456 "ae" =~ /^[aeiou]+$/ # Match, due to the quantifier.
457
1f59b283
KW
458 -------
459
8f0cd35a
KW
460* There are two exceptions to a bracketed character class matching a
461single character only. Each requires special handling by Perl to make
462things work:
463
464=over
465
466=item *
467
468When the class is to match caselessly under C</i> matching rules, and a
469character that is explicitly mentioned inside the class matches a
1f59b283 470multiple-character sequence caselessly under Unicode rules, the class
8f0cd35a
KW
471will also match that sequence. For example, Unicode says that the
472letter C<LATIN SMALL LETTER SHARP S> should match the sequence C<ss>
473under C</i> rules. Thus,
1f59b283
KW
474
475 'ss' =~ /\A\N{LATIN SMALL LETTER SHARP S}\z/i # Matches
476 'ss' =~ /\A[aeioust\N{LATIN SMALL LETTER SHARP S}]\z/i # Matches
477
8f0cd35a
KW
478For this to happen, the class must not be inverted (see L</Negation>)
479and the character must be explicitly specified, and not be part of a
480multi-character range (not even as one of its endpoints). (L</Character
481Ranges> will be explained shortly.) Therefore,
9d53c457 482
eb9e3b14
KW
483 'ss' =~ /\A[\0-\x{ff}]\z/ui # Doesn't match
484 'ss' =~ /\A[\0-\N{LATIN SMALL LETTER SHARP S}]\z/ui # No match
485 'ss' =~ /\A[\xDF-\xDF]\z/ui # Matches on ASCII platforms, since
8f0cd35a
KW
486 # \XDF is LATIN SMALL LETTER SHARP S,
487 # and the range is just a single
488 # element
9d53c457
KW
489
490Note that it isn't a good idea to specify these types of ranges anyway.
491
8f0cd35a
KW
492=item *
493
494Some names known to C<\N{...}> refer to a sequence of multiple characters,
495instead of the usual single character. When one of these is included in
496the class, the entire sequence is matched. For example,
497
498 "\N{TAMIL LETTER KA}\N{TAMIL VOWEL SIGN AU}"
499 =~ / ^ [\N{TAMIL SYLLABLE KAU}] $ /x;
500
501matches, because C<\N{TAMIL SYLLABLE KAU}> is a named sequence
502consisting of the two characters matched against. Like the other
eb9e3b14 503instance where a bracketed class can match multiple characters, and for
8f0cd35a
KW
504similar reasons, the class must not be inverted, and the named sequence
505may not appear in a range, even one where it is both endpoints. If
506these happen, it is a fatal error if the character class is within an
507extended L<C<(?[...])>|/Extended Bracketed Character Classes>
508class; and only the first code point is used (with
509a C<regexp>-type warning raised) otherwise.
510
511=back
512
8a118206
RGS
513=head3 Special Characters Inside a Bracketed Character Class
514
515Most characters that are meta characters in regular expressions (that
df225385 516is, characters that carry a special meaning like C<.>, C<*>, or C<(>) lose
8a118206
RGS
517their special meaning and can be used inside a character class without
518the need to escape them. For instance, C<[()]> matches either an opening
519parenthesis, or a closing parenthesis, and the parens inside the character
520class don't group or capture.
521
522Characters that may carry a special meaning inside a character class are:
523C<\>, C<^>, C<->, C<[> and C<]>, and are discussed below. They can be
524escaped with a backslash, although this is sometimes not needed, in which
525case the backslash may be omitted.
526
527The sequence C<\b> is special inside a bracketed character class. While
6b83a163 528outside the character class, C<\b> is an assertion indicating a point
8a118206
RGS
529that does not have either two word characters or two non-word characters
530on either side, inside a bracketed character class, C<\b> matches a
531backspace character.
532
df225385
KW
533The sequences
534C<\a>,
535C<\c>,
536C<\e>,
537C<\f>,
538C<\n>,
e526e8bb 539C<\N{I<NAME>}>,
765fa144 540C<\N{U+I<hex char>}>,
df225385
KW
541C<\r>,
542C<\t>,
543and
544C<\x>
06ee63cd 545are also special and have the same meanings as they do outside a
eb9e3b14 546bracketed character class.
df225385 547
ea449505
KW
548Also, a backslash followed by two or three octal digits is considered an octal
549number.
df225385 550
6b83a163
KW
551A C<[> is not special inside a character class, unless it's the start of a
552POSIX character class (see L</POSIX Character Classes> below). It normally does
553not need escaping.
8a118206 554
6b83a163
KW
555A C<]> is normally either the end of a POSIX character class (see
556L</POSIX Character Classes> below), or it signals the end of the bracketed
557character class. If you want to include a C<]> in the set of characters, you
558must generally escape it.
b6538e4f 559
8a118206
RGS
560However, if the C<]> is the I<first> (or the second if the first
561character is a caret) character of a bracketed character class, it
562does not denote the end of the class (as you cannot have an empty class)
563and is considered part of the set of characters that can be matched without
564escaping.
565
566Examples:
567
568 "+" =~ /[+?*]/ # Match, "+" in a character class is not special.
f321be7e 569 "\cH" =~ /[\b]/ # Match, \b inside in a character class.
c1c4ae3a 570 # is equivalent to a backspace.
8a118206
RGS
571 "]" =~ /[][]/ # Match, as the character class contains.
572 # both [ and ].
573 "[]" =~ /[[]]/ # Match, the pattern contains a character class
574 # containing just ], and the character class is
575 # followed by a ].
576
577=head3 Character Ranges
578
579It is not uncommon to want to match a range of characters. Luckily, instead
b6538e4f 580of listing all characters in the range, one may use the hyphen (C<->).
8a118206 581If inside a bracketed character class you have two characters separated
b6538e4f 582by a hyphen, it's treated as if all characters between the two were in
8a118206 583the class. For instance, C<[0-9]> matches any ASCII digit, and C<[a-m]>
e2cfb18c 584matches any lowercase letter from the first half of the ASCII alphabet.
8a118206
RGS
585
586Note that the two characters on either side of the hyphen are not
765fa144 587necessarily both letters or both digits. Any character is possible,
8a118206 588although not advisable. C<['-?]> contains a range of characters, but
b6538e4f 589most people will not know which characters that means. Furthermore,
8a118206
RGS
590such ranges may lead to portability problems if the code has to run on
591a platform that uses a different character set, such as EBCDIC.
592
ea449505
KW
593If a hyphen in a character class cannot syntactically be part of a range, for
594instance because it is the first or the last character of the character class,
b6538e4f
TC
595or if it immediately follows a range, the hyphen isn't special, and so is
596considered a character to be matched literally. If you want a hyphen in
597your set of characters to be matched and its position in the class is such
598that it could be considered part of a range, you must escape that hyphen
599with a backslash.
8a118206
RGS
600
601Examples:
602
603 [a-z] # Matches a character that is a lower case ASCII letter.
c1c4ae3a
KW
604 [a-fz] # Matches any letter between 'a' and 'f' (inclusive) or
605 # the letter 'z'.
8a118206
RGS
606 [-z] # Matches either a hyphen ('-') or the letter 'z'.
607 [a-f-m] # Matches any letter between 'a' and 'f' (inclusive), the
608 # hyphen ('-'), or the letter 'm'.
609 ['-?] # Matches any of the characters '()*+,-./0123456789:;<=>?
610 # (But not on an EBCDIC platform).
c7d25594
KW
611 [\N{APOSTROPHE}-\N{QUESTION MARK}]
612 # Matches any of the characters '()*+,-./0123456789:;<=>?
613 # even on an EBCDIC platform.
614 [\N{U+27}-\N{U+3F}] # Same. (U+27 is "'", and U+3F is "?"
615
616As the final two examples above show, you can achieve portablity to
617non-ASCII platforms by using the C<\N{...}> form for the range
618endpoints. These indicate that the specified range is to be interpreted
619using Unicode values, so C<[\N{U+27}-\N{U+3F}]> means to match
620C<\N{U+27}>, C<\N{U+28}>, C<\N{U+29}>, ..., C<\N{U+3D}>, C<\N{U+3E}>,
621and C<\N{U+3F}>, whatever the native code point versions for those are.
622
623Perl also guarantees that the ranges C<A-Z>, C<a-z>, C<0-9>, and any
09e43397 624subranges of these match what an English-only speaker would expect them
c7d25594
KW
625to match on any platform. That is, C<[A-Z]> matches the 26 ASCII
626uppercase letters;
09e43397
KW
627C<[a-z]> matches the 26 lowercase letters; and C<[0-9]> matches the 10
628digits. Subranges, like C<[h-k]>, match correspondingly, in this case
629just the four letters C<"h">, C<"i">, C<"j">, and C<"k">. This is the
630natural behavior on ASCII platforms where the code points (ordinal
631values) for C<"h"> through C<"k"> are consecutive integers (0x68 through
6320x6B). But special handling to achieve this may be needed on platforms
633with a non-ASCII native character set. For example, on EBCDIC
634platforms, the code point for C<"h"> is 0x88, C<"i"> is 0x89, C<"j"> is
6350x91, and C<"k"> is 0x92. Perl specially treats C<[h-k]> to exclude the
636seven code points in the gap: 0x8A through 0x90. This special handling is
637only invoked when the range is a subrange of one of the ASCII uppercase,
638lowercase, and digit ranges, AND each end of the range is expressed
639either as a literal, like C<"A">, or as a named character (C<\N{...}>,
640including the C<\N{U+...> form).
641
642EBCDIC Examples:
643
644 [i-j] # Matches either "i" or "j"
645 [i-\N{LATIN SMALL LETTER J}] # Same
646 [i-\N{U+6A}] # Same
647 [\N{U+69}-\N{U+6A}] # Same
648 [\x{89}-\x{91}] # Matches 0x89 ("i"), 0x8A .. 0x90, 0x91 ("j")
649 [i-\x{91}] # Same
650 [\x{89}-j] # Same
651 [i-J] # Matches, 0x89 ("i") .. 0xC1 ("J"); special
652 # handling doesn't apply because range is mixed
653 # case
8a118206
RGS
654
655=head3 Negation
656
657It is also possible to instead list the characters you do not want to
658match. You can do so by using a caret (C<^>) as the first character in the
b6538e4f 659character class. For instance, C<[^a-z]> matches any character that is not a
e2cfb18c
KW
660lowercase ASCII letter, which therefore includes more than a million
661Unicode code points. The class is said to be "negated" or "inverted".
8a118206
RGS
662
663This syntax make the caret a special character inside a bracketed character
664class, but only if it is the first character of the class. So if you want
82206b5e 665the caret as one of the characters to match, either escape the caret or
e2cfb18c 666else don't list it first.
8a118206 667
1f59b283 668In inverted bracketed character classes, Perl ignores the Unicode rules
8f0cd35a
KW
669that normally say that named sequence, and certain characters should
670match a sequence of multiple characters use under caseless C</i>
671matching. Following those rules could lead to highly confusing
672situations:
1f59b283 673
582da942 674 "ss" =~ /^[^\xDF]+$/ui; # Matches!
1f59b283
KW
675
676This should match any sequences of characters that aren't C<\xDF> nor
677what C<\xDF> matches under C</i>. C<"s"> isn't C<\xDF>, but Unicode
678says that C<"ss"> is what C<\xDF> matches under C</i>. So which one
679"wins"? Do you fail the match because the string has C<ss> or accept it
582da942 680because it has an C<s> followed by another C<s>? Perl has chosen the
8f0cd35a 681latter. (See note in L</Bracketed Character Classes> above.)
1f59b283 682
8a118206
RGS
683Examples:
684
685 "e" =~ /[^aeiou]/ # No match, the 'e' is listed.
686 "x" =~ /[^aeiou]/ # Match, as 'x' isn't a lowercase vowel.
687 "^" =~ /[^^]/ # No match, matches anything that isn't a caret.
688 "^" =~ /[x^]/ # Match, caret is not special here.
689
690=head3 Backslash Sequences
691
ea449505 692You can put any backslash sequence character class (with the exception of
765fa144 693C<\N> and C<\R>) inside a bracketed character class, and it will act just
b6538e4f
TC
694as if you had put all characters matched by the backslash sequence inside the
695character class. For instance, C<[a-f\d]> matches any decimal digit, or any
6b83a163
KW
696of the lowercase letters between 'a' and 'f' inclusive.
697
698C<\N> within a bracketed character class must be of the forms C<\N{I<name>}>
765fa144 699or C<\N{U+I<hex char>}>, and NOT be the form that matches non-newlines,
6b83a163
KW
700for the same reason that a dot C<.> inside a bracketed character class loses
701its special meaning: it matches nearly anything, which generally isn't what you
702want to happen.
df225385 703
8a118206
RGS
704
705Examples:
706
707 /[\p{Thai}\d]/ # Matches a character that is either a Thai
708 # character, or a digit.
709 /[^\p{Arabic}()]/ # Matches a character that is neither an Arabic
710 # character, nor a parenthesis.
711
712Backslash sequence character classes cannot form one of the endpoints
6b83a163
KW
713of a range. Thus, you can't say:
714
715 /[\p{Thai}-\d]/ # Wrong!
8a118206 716
6b83a163 717=head3 POSIX Character Classes
ea449505 718X<character class> X<\p> X<\p{}>
ea449505
KW
719X<alpha> X<alnum> X<ascii> X<blank> X<cntrl> X<digit> X<graph>
720X<lower> X<print> X<punct> X<space> X<upper> X<word> X<xdigit>
8a118206 721
d66e1f56 722POSIX character classes have the form C<[:class:]>, where I<class> is the
6b83a163 723name, and the C<[:> and C<:]> delimiters. POSIX character classes only appear
8a118206 724I<inside> bracketed character classes, and are a convenient and descriptive
82206b5e 725way of listing a group of characters.
6b83a163
KW
726
727Be careful about the syntax,
8a118206
RGS
728
729 # Correct:
730 $string =~ /[[:alpha:]]/
731
732 # Incorrect (will warn):
733 $string =~ /[:alpha:]/
734
735The latter pattern would be a character class consisting of a colon,
736and the letters C<a>, C<l>, C<p> and C<h>.
d66e1f56 737
82206b5e 738POSIX character classes can be part of a larger bracketed character class.
b6538e4f 739For example,
ea449505
KW
740
741 [01[:alpha:]%]
742
743is valid and matches '0', '1', any alphabetic character, and the percent sign.
8a118206
RGS
744
745Perl recognizes the following POSIX character classes:
746
ea449505 747 alpha Any alphabetical character ("[A-Za-z]").
48cbae4f 748 alnum Any alphanumeric character ("[A-Za-z0-9]").
ea449505 749 ascii Any character in the ASCII character set.
ea8b8ad2 750 blank A GNU extension, equal to a space or a horizontal tab ("\t").
ea449505
KW
751 cntrl Any control character. See Note [2] below.
752 digit Any decimal digit ("[0-9]"), equivalent to "\d".
753 graph Any printable character, excluding a space. See Note [3] below.
754 lower Any lowercase character ("[a-z]").
755 print Any printable character, including a space. See Note [4] below.
c1c4ae3a 756 punct Any graphical character excluding "word" characters. Note [5].
d28d8023
KW
757 space Any whitespace character. "\s" including the vertical tab
758 ("\cK").
ea449505
KW
759 upper Any uppercase character ("[A-Z]").
760 word A Perl extension ("[A-Za-z0-9_]"), equivalent to "\w".
761 xdigit Any hexadecimal digit ("[0-9a-fA-F]").
762
763Most POSIX character classes have two Unicode-style C<\p> property
764counterparts. (They are not official Unicode properties, but Perl extensions
765derived from official Unicode properties.) The table below shows the relation
766between POSIX character classes and these counterparts.
767
768One counterpart, in the column labelled "ASCII-range Unicode" in
b6538e4f 769the table, matches only characters in the ASCII character set.
ea449505
KW
770
771The other counterpart, in the column labelled "Full-range Unicode", matches any
772appropriate characters in the full Unicode character set. For example,
b6538e4f 773C<\p{Alpha}> matches not just the ASCII alphabetic characters, but any
82206b5e 774character in the entire Unicode character set considered alphabetic.
582da942 775An entry in the column labelled "backslash sequence" is a (short)
5db9882c 776equivalent.
ea449505 777
cbc24f92
KW
778 [[:...:]] ASCII-range Full-range backslash Note
779 Unicode Unicode sequence
ea449505 780 -----------------------------------------------------
cbc24f92
KW
781 alpha \p{PosixAlpha} \p{XPosixAlpha}
782 alnum \p{PosixAlnum} \p{XPosixAlnum}
82206b5e 783 ascii \p{ASCII}
cbc24f92
KW
784 blank \p{PosixBlank} \p{XPosixBlank} \h [1]
785 or \p{HorizSpace} [1]
786 cntrl \p{PosixCntrl} \p{XPosixCntrl} [2]
787 digit \p{PosixDigit} \p{XPosixDigit} \d
788 graph \p{PosixGraph} \p{XPosixGraph} [3]
789 lower \p{PosixLower} \p{XPosixLower}
790 print \p{PosixPrint} \p{XPosixPrint} [4]
791 punct \p{PosixPunct} \p{XPosixPunct} [5]
792 \p{PerlSpace} \p{XPerlSpace} \s [6]
793 space \p{PosixSpace} \p{XPosixSpace} [6]
794 upper \p{PosixUpper} \p{XPosixUpper}
795 word \p{PosixWord} \p{XPosixWord} \w
82206b5e 796 xdigit \p{PosixXDigit} \p{XPosixXDigit}
8a118206
RGS
797
798=over 4
799
ea449505
KW
800=item [1]
801
802C<\p{Blank}> and C<\p{HorizSpace}> are synonyms.
803
804=item [2]
8a118206 805
ea449505 806Control characters don't produce output as such, but instead usually control
b6538e4f 807the terminal somehow: for example, newline and backspace are control characters.
82206b5e 808In the ASCII range, characters whose code points are between 0 and 31 inclusive,
ea449505 809plus 127 (C<DEL>) are control characters.
8a118206 810
ea449505 811=item [3]
8a118206
RGS
812
813Any character that is I<graphical>, that is, visible. This class consists
b6538e4f 814of all alphanumeric characters and all punctuation characters.
8a118206 815
ea449505 816=item [4]
8a118206 817
b6538e4f
TC
818All printable characters, which is the set of all graphical characters
819plus those whitespace characters which are not also controls.
ea449505 820
b6dac59a 821=item [5]
ea449505 822
b6538e4f 823C<\p{PosixPunct}> and C<[[:punct:]]> in the ASCII range match all
ea449505
KW
824non-controls, non-alphanumeric, non-space characters:
825C<[-!"#$%&'()*+,./:;<=E<gt>?@[\\\]^_`{|}~]> (although if a locale is in effect,
826it could alter the behavior of C<[[:punct:]]>).
827
cbc24f92
KW
828The similarly named property, C<\p{Punct}>, matches a somewhat different
829set in the ASCII range, namely
0be9b861
KW
830C<[-!"#%&'()*,./:;?@[\\\]_{}]>. That is, it is missing the nine
831characters C<[$+E<lt>=E<gt>^`|~]>.
6c5a041f
KW
832This is because Unicode splits what POSIX considers to be punctuation into two
833categories, Punctuation and Symbols.
834
e2cfb18c 835C<\p{XPosixPunct}> and (under Unicode rules) C<[[:punct:]]>, match what
765fa144
KW
836C<\p{PosixPunct}> matches in the ASCII range, plus what C<\p{Punct}>
837matches. This is different than strictly matching according to
838C<\p{Punct}>. Another way to say it is that
82206b5e
KW
839if Unicode rules are in effect, C<[[:punct:]]> matches all characters
840that Unicode considers punctuation, plus all ASCII-range characters that
841Unicode considers symbols.
8a118206 842
ea449505 843=item [6]
8a118206 844
7fa2fdc0 845C<\p{XPerlSpace}> and C<\p{Space}> match identically starting with Perl
d28d8023 846v5.18. In earlier versions, these differ only in that in non-locale
7fa2fdc0 847matching, C<\p{XPerlSpace}> does not match the vertical tab, C<\cK>.
d28d8023 848Same for the two ASCII-only range forms.
8a118206
RGS
849
850=back
851
ab6199be
KW
852There are various other synonyms that can be used besides the names
853listed in the table. For example, C<\p{PosixAlpha}> can be written as
854C<\p{Alpha}>. All are listed in
d66e1f56 855L<perluniprops/Properties accessible through \p{} and \P{}>.
ab6199be
KW
856
857Both the C<\p> counterparts always assume Unicode rules are in effect.
858On ASCII platforms, this means they assume that the code points from 128
859to 255 are Latin-1, and that means that using them under locale rules is
860unwise unless the locale is guaranteed to be Latin-1 or UTF-8. In contrast, the
861POSIX character classes are useful under locale rules. They are
862affected by the actual rules in effect, as follows:
863
864=over
865
866=item If the C</a> modifier, is in effect ...
867
868Each of the POSIX classes matches exactly the same as their ASCII-range
869counterparts.
870
871=item otherwise ...
872
873=over
874
875=item For code points above 255 ...
876
877The POSIX class matches the same as its Full-range counterpart.
878
879=item For code points below 256 ...
880
881=over
882
883=item if locale rules are in effect ...
884
a145a423
KW
885The POSIX class matches according to the locale, except:
886
887=over
888
889=item C<word>
890
891also includes the platform's native underscore character, no matter what
8129baca 892the locale is.
ab6199be 893
a145a423
KW
894=item C<ascii>
895
896on platforms that don't have the POSIX C<ascii> extension, this matches
897just the platform's native ASCII-range characters.
898
899=item C<blank>
900
901on platforms that don't have the POSIX C<blank> extension, this matches
902just the platform's native tab and space characters.
903
904=back
905
4b9734bf 906=item if Unicode rules are in effect ...
ab6199be
KW
907
908The POSIX class matches the same as the Full-range counterpart.
909
910=item otherwise ...
911
912The POSIX class matches the same as the ASCII range counterpart.
913
914=back
915
916=back
917
918=back
919
920Which rules apply are determined as described in
921L<perlre/Which character set modifier is in effect?>.
922
923It is proposed to change this behavior in a future release of Perl so that
924whether or not Unicode rules are in effect would not change the
4b9734bf 925behavior: Outside of locale, the POSIX classes
ab6199be
KW
926would behave like their ASCII-range counterparts. If you wish to
927comment on this proposal, send email to C<perl5-porters@perl.org>.
cbc24f92 928
1f59b283 929=head4 Negation of POSIX character classes
ea449505 930X<character class, negation>
8a118206
RGS
931
932A Perl extension to the POSIX character class is the ability to
933negate it. This is done by prefixing the class name with a caret (C<^>).
934Some examples:
935
ea449505
KW
936 POSIX ASCII-range Full-range backslash
937 Unicode Unicode sequence
938 -----------------------------------------------------
cbc24f92
KW
939 [[:^digit:]] \P{PosixDigit} \P{XPosixDigit} \D
940 [[:^space:]] \P{PosixSpace} \P{XPosixSpace}
941 \P{PerlSpace} \P{XPerlSpace} \S
942 [[:^word:]] \P{PerlWord} \P{XPosixWord} \W
943
765fa144 944The backslash sequence can mean either ASCII- or Full-range Unicode,
82206b5e 945depending on various factors as described in L<perlre/Which character set modifier is in effect?>.
8a118206
RGS
946
947=head4 [= =] and [. .]
948
b6538e4f 949Perl recognizes the POSIX character classes C<[=class=]> and
82206b5e 950C<[.class.]>, but does not (yet?) support them. Any attempt to use
b6538e4f 951either construct raises an exception.
8a118206
RGS
952
953=head4 Examples
954
955 /[[:digit:]]/ # Matches a character that is a digit.
956 /[01[:lower:]]/ # Matches a character that is either a
957 # lowercase letter, or '0' or '1'.
c1c4ae3a 958 /[[:digit:][:^xdigit:]]/ # Matches a character that can be anything
bc943be5
KW
959 # except the letters 'a' to 'f' and 'A' to
960 # 'F'. This is because the main character
961 # class is composed of two POSIX character
962 # classes that are ORed together, one that
963 # matches any digit, and the other that
964 # matches anything that isn't a hex digit.
965 # The OR adds the digits, leaving only the
966 # letters 'a' to 'f' and 'A' to 'F' excluded.
572224ce
KW
967
968=head3 Extended Bracketed Character Classes
969X<character class>
970X<set operations>
971
972This is a fancy bracketed character class that can be used for more
973readable and less error-prone classes, and to perform set operations,
974such as intersection. An example is
975
976 /(?[ \p{Thai} & \p{Digit} ])/
977
978This will match all the digit characters that are in the Thai script.
979
980This is an experimental feature available starting in 5.18, and is
981subject to change as we gain field experience with it. Any attempt to
982use it will raise a warning, unless disabled via
983
984 no warnings "experimental::regex_sets";
985
986Comments on this feature are welcome; send email to
987C<perl5-porters@perl.org>.
988
989We can extend the example above:
990
991 /(?[ ( \p{Thai} + \p{Lao} ) & \p{Digit} ])/
992
993This matches digits that are in either the Thai or Laotian scripts.
994
995Notice the white space in these examples. This construct always has
d66e1f56 996the C<E<sol>x> modifier turned on within it.
572224ce
KW
997
998The available binary operators are:
999
1000 & intersection
1001 + union
1002 | another name for '+', hence means union
1003 - subtraction (the result matches the set consisting of those
1004 code points matched by the first operand, excluding any that
1005 are also matched by the second operand)
1006 ^ symmetric difference (the union minus the intersection). This
1007 is like an exclusive or, in that the result is the set of code
1008 points that are matched by either, but not both, of the
1009 operands.
1010
1011There is one unary operator:
1012
1013 ! complement
1014
1015All the binary operators left associate, and are of equal precedence.
1016The unary operator right associates, and has higher precedence. Use
1017parentheses to override the default associations. Some feedback we've
1018received indicates a desire for intersection to have higher precedence
1019than union. This is something that feedback from the field may cause us
1020to change in future releases; you may want to parenthesize copiously to
1021avoid such changes affecting your code, until this feature is no longer
1022considered experimental.
1023
1024The main restriction is that everything is a metacharacter. Thus,
1025you cannot refer to single characters by doing something like this:
1026
1027 /(?[ a + b ])/ # Syntax error!
1028
1029The easiest way to specify an individual typable character is to enclose
1030it in brackets:
1031
1032 /(?[ [a] + [b] ])/
1033
1034(This is the same thing as C<[ab]>.) You could also have said the
1035equivalent:
1036
1037 /(?[[ a b ]])/
1038
de36fb2e
KW
1039(You can, of course, specify single characters by using, C<\x{...}>,
1040C<\N{...}>, etc.)
572224ce
KW
1041
1042This last example shows the use of this construct to specify an ordinary
1043bracketed character class without additional set operations. Note the
1044white space within it; C<E<sol>x> is turned on even within bracketed
1045character classes, except you can't have comments inside them. Hence,
1046
1047 (?[ [#] ])
1048
1049matches the literal character "#". To specify a literal white space character,
1050you can escape it with a backslash, like:
1051
1052 /(?[ [ a e i o u \ ] ])/
1053
1054This matches the English vowels plus the SPACE character.
1055All the other escapes accepted by normal bracketed character classes are
1056accepted here as well; but unrecognized escapes that generate warnings
1057in normal classes are fatal errors here.
1058
1059All warnings from these class elements are fatal, as well as some
1060practices that don't currently warn. For example you cannot say
1061
1062 /(?[ [ \xF ] ])/ # Syntax error!
1063
1064You have to have two hex digits after a braceless C<\x> (use a leading
1065zero to make two). These restrictions are to lower the incidence of
1066typos causing the class to not match what you thought it would.
1067
f194034a
KW
1068If a regular bracketed character class contains a C<\p{}> or C<\P{}> and
1069is matched against a non-Unicode code point, a warning may be
1070raised, as the result is not Unicode-defined. No such warning will come
1071when using this extended form.
1072
572224ce
KW
1073The final difference between regular bracketed character classes and
1074these, is that it is not possible to get these to match a
1075multi-character fold. Thus,
1076
1077 /(?[ [\xDF] ])/iu
1078
1079does not match the string C<ss>.
1080
1081You don't have to enclose POSIX class names inside double brackets,
1082hence both of the following work:
1083
1084 /(?[ [:word:] - [:lower:] ])/
1085 /(?[ [[:word:]] - [[:lower:]] ])/
1086
1087Any contained POSIX character classes, including things like C<\w> and C<\D>
1088respect the C<E<sol>a> (and C<E<sol>aa>) modifiers.
1089
1090C<< (?[ ]) >> is a regex-compile-time construct. Any attempt to use
1091something which isn't knowable at the time the containing regular
1092expression is compiled is a fatal error. In practice, this means
11a9b3e0 1093just three limitations:
572224ce
KW
1094
1095=over 4
1096
1097=item 1
1098
1099This construct cannot be used within the scope of
1100C<use locale> (or the C<E<sol>l> regex modifier).
1101
1102=item 2
1103
1104Any
1105L<user-defined property|perlunicode/"User-Defined Character Properties">
1106used must be already defined by the time the regular expression is
1107compiled (but note that this construct can be used instead of such
1108properties).
1109
1110=item 3
1111
1112A regular expression that otherwise would compile
1113using C<E<sol>d> rules, and which uses this construct will instead
1114use C<E<sol>u>. Thus this construct tells Perl that you don't want
1115C<E<sol>d> rules for the entire regular expression containing it.
1116
1117=back
1118
572224ce
KW
1119Note that skipping white space applies only to the interior of this
1120construct. There must not be any space between any of the characters
1121that form the initial C<(?[>. Nor may there be space between the
1122closing C<])> characters.
1123
11a9b3e0 1124Just as in all regular expressions, the pattern can be built up by
572224ce
KW
1125including variables that are interpolated at regex compilation time.
1126Care must be taken to ensure that you are getting what you expect. For
1127example:
1128
1129 my $thai_or_lao = '\p{Thai} + \p{Lao}';
1130 ...
1131 qr/(?[ \p{Digit} & $thai_or_lao ])/;
1132
1133compiles to
1134
1135 qr/(?[ \p{Digit} & \p{Thai} + \p{Lao} ])/;
1136
1137But this does not have the effect that someone reading the code would
1138likely expect, as the intersection applies just to C<\p{Thai}>,
1139excluding the Laotian. Pitfalls like this can be avoided by
1140parenthesizing the component pieces:
1141
1142 my $thai_or_lao = '( \p{Thai} + \p{Lao} )';
1143
1144But any modifiers will still apply to all the components:
1145
1146 my $lower = '\p{Lower} + \p{Digit}';
1147 qr/(?[ \p{Greek} & $lower ])/i;
1148
1149matches upper case things. You can avoid surprises by making the
1150components into instances of this construct by compiling them:
1151
1152 my $thai_or_lao = qr/(?[ \p{Thai} + \p{Lao} ])/;
1153 my $lower = qr/(?[ \p{Lower} + \p{Digit} ])/;
1154
1155When these are embedded in another pattern, what they match does not
1156change, regardless of parenthesization or what modifiers are in effect
1157in that outer pattern.
1158
1159Due to the way that Perl parses things, your parentheses and brackets
1160may need to be balanced, even including comments. If you run into any
1161examples, please send them to C<perlbug@perl.org>, so that we can have a
1162concrete example for this man page.
1163
1164We may change it so that things that remain legal uses in normal bracketed
1165character classes might become illegal within this experimental
1166construct. One proposal, for example, is to forbid adjacent uses of the
1167same character, as in C<(?[ [aa] ])>. The motivation for such a change
1168is that this usage is likely a typo, as the second "a" adds nothing.