This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Updates to perldelta for all the updated CPAN modules.
[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
29character, except for the newline. The 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
32locally with C<(?s)>. (The experimental C<\N> backslash sequence, described
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.
6b83a163
KW
71 \N Match a character that isn't a newline. Experimental.
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
77C<\N> is new in 5.12, and is experimental. It, like the dot, matches any
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>.
82206b5e
KW
92If the C</a> modifier in effect, it matches [0-9]. Otherwise, it
93matches anything that is matched by C<\p{Digit}>, which includes [0-9].
94(An unlikely possible exception is that under locale matching rules, the
95current locale might not have [0-9] matched by C<\d>, and/or might match
96other characters whose code point is less than 256. Such a locale
97definition would be in violation of the C language standard, but Perl
98doesn't currently assume anything in regard to this.)
99
100What this means is that unless the C</a> modifier is in effect C<\d> not
101only matches the digits '0' - '9', but also Arabic, Devanagari, and
102digits from other languages. This may cause some confusion, and some
103security issues.
104
105Some digits that C<\d> matches look like some of the [0-9] ones, but
106have different values. For example, BENGALI DIGIT FOUR (U+09EA) looks
107very much like an ASCII DIGIT EIGHT (U+0038). An application that
108is expecting only the ASCII digits might be misled, or if the match is
109C<\d+>, the matched string might contain a mixture of digits from
110different writing systems that look like they signify a number different
e397bccf
KW
111than they actually do. L<Unicode::UCDE<sol>num()|Unicode::UCD/num> can
112be used to safely
82206b5e
KW
113calculate the value, returning C<undef> if the input string contains
114such a mixture.
115
116What C<\p{Digit}> means (and hence C<\d> except under the C</a>
117modifier) is C<\p{General_Category=Decimal_Number}>, or synonymously,
118C<\p{General_Category=Digit}>. Starting with Unicode version 4.1, this
119is the same set of characters matched by C<\p{Numeric_Type=Decimal}>.
6b83a163
KW
120But Unicode also has a different property with a similar name,
121C<\p{Numeric_Type=Digit}>, which matches a completely different set of
82206b5e
KW
122characters. These characters are things such as C<CIRCLED DIGIT ONE>
123or subscripts, or are from writing systems that lack all ten digits.
6b83a163 124
82206b5e
KW
125The design intent is for C<\d> to exactly match the set of characters
126that can safely be used with "normal" big-endian positional decimal
127syntax, where, for example 123 means one 'hundred', plus two 'tens',
128plus three 'ones'. This positional notation does not necessarily apply
129to characters that match the other type of "digit",
130C<\p{Numeric_Type=Digit}>, and so C<\d> doesn't match them.
6b83a163 131
e2cfb18c 132The Tamil digits (U+0BE6 - U+0BEF) can also legally be
82206b5e
KW
133used in old-style Tamil numbers in which they would appear no more than
134one in a row, separated by characters that mean "times 10", "times 100",
135etc. (See L<http://www.unicode.org/notes/tn21>.)
8a118206 136
b6538e4f 137Any character not matched by C<\d> is matched by C<\D>.
8a118206
RGS
138
139=head3 Word characters
140
ea449505 141A C<\w> matches a single alphanumeric character (an alphabetic character, or a
d35dd6c6
KW
142decimal digit) or a connecting punctuation character, such as an
143underscore ("_"). It does not match a whole word. To match a whole
82206b5e 144word, use C<\w+>. This isn't the same thing as matching an English word, but
765fa144 145in the ASCII range it is the same as a string of Perl-identifier
82206b5e
KW
146characters.
147
148=over
149
150=item If the C</a> modifier is in effect ...
151
152C<\w> matches the 63 characters [a-zA-Z0-9_].
153
154=item otherwise ...
155
156=over
157
158=item For code points above 255 ...
159
160C<\w> matches the same as C<\p{Word}> matches in this range. That is,
161it matches Thai letters, Greek letters, etc. This includes connector
d35dd6c6 162punctuation (like the underscore) which connect two words together, or
b6538e4f 163diacritics, such as a C<COMBINING TILDE> and the modifier letters, which
82206b5e
KW
164are generally used to add auxiliary markings to letters.
165
166=item For code points below 256 ...
167
168=over
169
170=item if locale rules are in effect ...
171
172C<\w> matches the platform's native underscore character plus whatever
173the locale considers to be alphanumeric.
174
175=item if Unicode rules are in effect or if on an EBCDIC platform ...
176
177C<\w> matches exactly what C<\p{Word}> matches.
178
179=item otherwise ...
180
181C<\w> matches [a-zA-Z0-9_].
182
183=back
184
185=back
186
187=back
188
189Which rules apply are determined as described in L<perlre/Which character set modifier is in effect?>.
8a118206 190
6b83a163
KW
191There are a number of security issues with the full Unicode list of word
192characters. See L<http://unicode.org/reports/tr36>.
193
194Also, for a somewhat finer-grained set of characters that are in programming
195language identifiers beyond the ASCII range, you may wish to instead use the
e2cfb18c
KW
196more customized L</Unicode Properties>, C<\p{ID_Start}>,
197C<\p{ID_Continue}>, C<\p{XID_Start}>, and C<\p{XID_Continue}>. See
198L<http://unicode.org/reports/tr31>.
6b83a163 199
b6538e4f 200Any character not matched by C<\w> is matched by C<\W>.
8a118206 201
ea449505
KW
202=head3 Whitespace
203
82206b5e
KW
204C<\s> matches any single character considered whitespace.
205
206=over
207
208=item If the C</a> modifier is in effect ...
209
210C<\s> matches the 5 characters [\t\n\f\r ]; that is, the horizontal tab,
211the newline, the form feed, the carriage return, and the space. (Note
212that it doesn't match the vertical tab, C<\cK> on ASCII platforms.)
213
214=item otherwise ...
215
216=over
217
218=item For code points above 255 ...
219
220C<\s> matches exactly the code points above 255 shown with an "s" column
221in the table below.
222
223=item For code points below 256 ...
224
225=over
226
227=item if locale rules are in effect ...
228
229C<\s> matches whatever the locale considers to be whitespace. Note that
230this is likely to include the vertical space, unlike non-locale C<\s>
231matching.
232
233=item if Unicode rules are in effect or if on an EBCDIC platform ...
234
235C<\s> matches exactly the characters shown with an "s" column in the
236table below.
237
238=item otherwise ...
239
240C<\s> matches [\t\n\f\r ].
241Note that this list doesn't include the non-breaking space.
242
243=back
244
245=back
246
247=back
248
249Which rules apply are determined as described in L<perlre/Which character set modifier is in effect?>.
8a118206 250
b6538e4f 251Any character not matched by C<\s> is matched by C<\S>.
8a118206 252
b6538e4f 253C<\h> matches any character considered horizontal whitespace;
82206b5e 254this includes the space and tab characters and several others
b6538e4f
TC
255listed in the table below. C<\H> matches any character
256not considered horizontal whitespace.
ea449505 257
b6538e4f 258C<\v> matches any character considered vertical whitespace;
82206b5e 259this includes the carriage return and line feed characters (newline)
b6538e4f
TC
260plus several other characters, all listed in the table below.
261C<\V> matches any character not considered vertical whitespace.
8a118206
RGS
262
263C<\R> matches anything that can be considered a newline under Unicode
264rules. It's not a character class, as it can match a multi-character
265sequence. Therefore, it cannot be used inside a bracketed character
ea449505
KW
266class; use C<\v> instead (vertical whitespace).
267Details are discussed in L<perlrebackslash>.
8a118206 268
82206b5e 269Note that unlike C<\s> (and C<\d> and C<\w>), C<\h> and C<\v> always match
b6538e4f
TC
270the same characters, without regard to other factors, such as whether the
271source string is in UTF-8 format.
8a118206 272
82206b5e
KW
273One might think that C<\s> is equivalent to C<[\h\v]>. This is not true.
274For example, the vertical tab (C<"\x0b">) is not matched by C<\s>, it is
275however considered vertical whitespace.
8a118206
RGS
276
277The following table is a complete listing of characters matched by
82206b5e 278C<\s>, C<\h> and C<\v> as of Unicode 6.0.
8a118206
RGS
279
280The first column gives the code point of the character (in hex format),
281the second column gives the (Unicode) name. The third column indicates
ea449505
KW
282by which class(es) the character is matched (assuming no locale or EBCDIC code
283page is in effect that changes the C<\s> matching).
8a118206 284
fc28d2a3
KW
285 0x0009 CHARACTER TABULATION h s
286 0x000a LINE FEED (LF) vs
287 0x000b LINE TABULATION v
288 0x000c FORM FEED (FF) vs
289 0x000d CARRIAGE RETURN (CR) vs
290 0x0020 SPACE h s
291 0x0085 NEXT LINE (NEL) vs [1]
292 0x00a0 NO-BREAK SPACE h s [1]
293 0x1680 OGHAM SPACE MARK h s
294 0x180e MONGOLIAN VOWEL SEPARATOR h s
295 0x2000 EN QUAD h s
296 0x2001 EM QUAD h s
297 0x2002 EN SPACE h s
298 0x2003 EM SPACE h s
299 0x2004 THREE-PER-EM SPACE h s
300 0x2005 FOUR-PER-EM SPACE h s
301 0x2006 SIX-PER-EM SPACE h s
302 0x2007 FIGURE SPACE h s
303 0x2008 PUNCTUATION SPACE h s
304 0x2009 THIN SPACE h s
305 0x200a HAIR SPACE h s
306 0x2028 LINE SEPARATOR vs
307 0x2029 PARAGRAPH SEPARATOR vs
308 0x202f NARROW NO-BREAK SPACE h s
309 0x205f MEDIUM MATHEMATICAL SPACE h s
310 0x3000 IDEOGRAPHIC SPACE h s
8a118206
RGS
311
312=over 4
313
314=item [1]
315
82206b5e
KW
316NEXT LINE and NO-BREAK SPACE may or may not match C<\s> depending
317on the rules in effect. See
318L<the beginning of this section|/Whitespace>.
8a118206
RGS
319
320=back
321
8a118206
RGS
322=head3 Unicode Properties
323
c1c4ae3a
KW
324C<\pP> and C<\p{Prop}> are character classes to match characters that fit given
325Unicode properties. One letter property names can be used in the C<\pP> form,
326with the property name following the C<\p>, otherwise, braces are required.
327When using braces, there is a single form, which is just the property name
328enclosed in the braces, and a compound form which looks like C<\p{name=value}>,
b6538e4f 329which means to match if the property "name" for the character has that particular
c1c4ae3a 330"value".
e1b711da
KW
331For instance, a match for a number can be written as C</\pN/> or as
332C</\p{Number}/>, or as C</\p{Number=True}/>.
333Lowercase letters are matched by the property I<Lowercase_Letter> which
e2cfb18c 334has the short form I<Ll>. They need the braces, so are written as C</\p{Ll}/> or
e1b711da
KW
335C</\p{Lowercase_Letter}/>, or C</\p{General_Category=Lowercase_Letter}/>
336(the underscores are optional).
337C</\pLl/> is valid, but means something different.
8a118206
RGS
338It matches a two character string: a letter (Unicode property C<\pL>),
339followed by a lowercase C<l>.
340
82206b5e
KW
341If neither the C</a> modifier nor locale rules are in effect, the use of
342a Unicode property will force the regular expression into using Unicode
343rules.
344
56ca34ca
KW
345Note that almost all properties are immune to case-insensitive matching.
346That is, adding a C</i> regular expression modifier does not change what
82206b5e 347they match. There are two sets that are affected. The first set is
56ca34ca
KW
348C<Uppercase_Letter>,
349C<Lowercase_Letter>,
350and C<Titlecase_Letter>,
351all of which match C<Cased_Letter> under C</i> matching.
b6538e4f 352The second set is
56ca34ca
KW
353C<Uppercase>,
354C<Lowercase>,
355and C<Titlecase>,
356all of which match C<Cased> under C</i> matching.
357(The difference between these sets is that some things, such as Roman
e2cfb18c 358numerals, come in both upper and lower case, so they are C<Cased>, but
b6538e4f 359aren't considered to be letters, so they aren't C<Cased_Letter>s. They're
82206b5e
KW
360actually C<Letter_Number>s.)
361This set also includes its subsets C<PosixUpper> and C<PosixLower>, both
e2cfb18c 362of which under C</i> match C<PosixAlpha>.
56ca34ca
KW
363
364For more details on Unicode properties, see L<perlunicode/Unicode
365Character Properties>; for a
e1b711da 366complete list of possible properties, see
56ca34ca
KW
367L<perluniprops/Properties accessible through \p{} and \P{}>,
368which notes all forms that have C</i> differences.
e1b711da 369It is also possible to define your own properties. This is discussed in
8a118206
RGS
370L<perlunicode/User-Defined Character Properties>.
371
8a118206
RGS
372=head4 Examples
373
374 "a" =~ /\w/ # Match, "a" is a 'word' character.
375 "7" =~ /\w/ # Match, "7" is a 'word' character as well.
376 "a" =~ /\d/ # No match, "a" isn't a digit.
377 "7" =~ /\d/ # Match, "7" is a digit.
ea449505 378 " " =~ /\s/ # Match, a space is whitespace.
8a118206
RGS
379 "a" =~ /\D/ # Match, "a" is a non-digit.
380 "7" =~ /\D/ # No match, "7" is not a non-digit.
ea449505 381 " " =~ /\S/ # No match, a space is not non-whitespace.
8a118206 382
ea449505
KW
383 " " =~ /\h/ # Match, space is horizontal whitespace.
384 " " =~ /\v/ # No match, space is not vertical whitespace.
385 "\r" =~ /\v/ # Match, a return is vertical whitespace.
8a118206
RGS
386
387 "a" =~ /\pL/ # Match, "a" is a letter.
388 "a" =~ /\p{Lu}/ # No match, /\p{Lu}/ matches upper case letters.
389
390 "\x{0e0b}" =~ /\p{Thai}/ # Match, \x{0e0b} is the character
391 # 'THAI CHARACTER SO SO', and that's in
392 # Thai Unicode class.
ea449505 393 "a" =~ /\P{Lao}/ # Match, as "a" is not a Laotian character.
8a118206 394
82206b5e
KW
395It is worth emphasizing that C<\d>, C<\w>, etc, match single characters, not
396complete numbers or words. To match a number (that consists of digits),
397use C<\d+>; to match a word, use C<\w+>. But be aware of the security
398considerations in doing so, as mentioned above.
8a118206
RGS
399
400=head2 Bracketed Character Classes
401
402The third form of character class you can use in Perl regular expressions
6b83a163 403is the bracketed character class. In its simplest form, it lists the characters
c1c4ae3a 404that may be matched, surrounded by square brackets, like this: C<[aeiou]>.
ea449505 405This matches one of C<a>, C<e>, C<i>, C<o> or C<u>. Like the other
1f59b283 406character classes, exactly one character is matched.* To match
ea449505 407a longer string consisting of characters mentioned in the character
6b83a163 408class, follow the character class with a L<quantifier|perlre/Quantifiers>. For
b6538e4f 409instance, C<[aeiou]+> matches one or more lowercase English vowels.
8a118206
RGS
410
411Repeating a character in a character class has no
412effect; it's considered to be in the set only once.
413
414Examples:
415
416 "e" =~ /[aeiou]/ # Match, as "e" is listed in the class.
417 "p" =~ /[aeiou]/ # No match, "p" is not listed in the class.
418 "ae" =~ /^[aeiou]$/ # No match, a character class only matches
419 # a single character.
420 "ae" =~ /^[aeiou]+$/ # Match, due to the quantifier.
421
1f59b283
KW
422 -------
423
88a4f93e 424* There is an exception to a bracketed character class matching only a
1f59b283
KW
425single character. When the class is to match caselessely under C</i>
426matching rules, and a character inside the class matches a
427multiple-character sequence caselessly under Unicode rules, the class
428(when not L<inverted|/Negation>) will also match that sequence. For
429example, Unicode says that the letter C<LATIN SMALL LETTER SHARP S>
430should match the sequence C<ss> under C</i> rules. Thus,
431
432 'ss' =~ /\A\N{LATIN SMALL LETTER SHARP S}\z/i # Matches
433 'ss' =~ /\A[aeioust\N{LATIN SMALL LETTER SHARP S}]\z/i # Matches
434
8a118206
RGS
435=head3 Special Characters Inside a Bracketed Character Class
436
437Most characters that are meta characters in regular expressions (that
df225385 438is, characters that carry a special meaning like C<.>, C<*>, or C<(>) lose
8a118206
RGS
439their special meaning and can be used inside a character class without
440the need to escape them. For instance, C<[()]> matches either an opening
441parenthesis, or a closing parenthesis, and the parens inside the character
442class don't group or capture.
443
444Characters that may carry a special meaning inside a character class are:
445C<\>, C<^>, C<->, C<[> and C<]>, and are discussed below. They can be
446escaped with a backslash, although this is sometimes not needed, in which
447case the backslash may be omitted.
448
449The sequence C<\b> is special inside a bracketed character class. While
6b83a163 450outside the character class, C<\b> is an assertion indicating a point
8a118206
RGS
451that does not have either two word characters or two non-word characters
452on either side, inside a bracketed character class, C<\b> matches a
453backspace character.
454
df225385
KW
455The sequences
456C<\a>,
457C<\c>,
458C<\e>,
459C<\f>,
460C<\n>,
e526e8bb 461C<\N{I<NAME>}>,
765fa144 462C<\N{U+I<hex char>}>,
df225385
KW
463C<\r>,
464C<\t>,
465and
466C<\x>
06ee63cd
KW
467are also special and have the same meanings as they do outside a
468bracketed character class. (However, inside a bracketed character
469class, if C<\N{I<NAME>}> expands to a sequence of characters, only the first
470one in the sequence is used, with a warning.)
df225385 471
ea449505
KW
472Also, a backslash followed by two or three octal digits is considered an octal
473number.
df225385 474
6b83a163
KW
475A C<[> is not special inside a character class, unless it's the start of a
476POSIX character class (see L</POSIX Character Classes> below). It normally does
477not need escaping.
8a118206 478
6b83a163
KW
479A C<]> is normally either the end of a POSIX character class (see
480L</POSIX Character Classes> below), or it signals the end of the bracketed
481character class. If you want to include a C<]> in the set of characters, you
482must generally escape it.
b6538e4f 483
8a118206
RGS
484However, if the C<]> is the I<first> (or the second if the first
485character is a caret) character of a bracketed character class, it
486does not denote the end of the class (as you cannot have an empty class)
487and is considered part of the set of characters that can be matched without
488escaping.
489
490Examples:
491
492 "+" =~ /[+?*]/ # Match, "+" in a character class is not special.
493 "\cH" =~ /[\b]/ # Match, \b inside in a character class
c1c4ae3a 494 # is equivalent to a backspace.
8a118206
RGS
495 "]" =~ /[][]/ # Match, as the character class contains.
496 # both [ and ].
497 "[]" =~ /[[]]/ # Match, the pattern contains a character class
498 # containing just ], and the character class is
499 # followed by a ].
500
501=head3 Character Ranges
502
503It is not uncommon to want to match a range of characters. Luckily, instead
b6538e4f 504of listing all characters in the range, one may use the hyphen (C<->).
8a118206 505If inside a bracketed character class you have two characters separated
b6538e4f 506by a hyphen, it's treated as if all characters between the two were in
8a118206 507the class. For instance, C<[0-9]> matches any ASCII digit, and C<[a-m]>
e2cfb18c 508matches any lowercase letter from the first half of the ASCII alphabet.
8a118206
RGS
509
510Note that the two characters on either side of the hyphen are not
765fa144 511necessarily both letters or both digits. Any character is possible,
8a118206 512although not advisable. C<['-?]> contains a range of characters, but
b6538e4f 513most people will not know which characters that means. Furthermore,
8a118206
RGS
514such ranges may lead to portability problems if the code has to run on
515a platform that uses a different character set, such as EBCDIC.
516
ea449505
KW
517If a hyphen in a character class cannot syntactically be part of a range, for
518instance because it is the first or the last character of the character class,
b6538e4f
TC
519or if it immediately follows a range, the hyphen isn't special, and so is
520considered a character to be matched literally. If you want a hyphen in
521your set of characters to be matched and its position in the class is such
522that it could be considered part of a range, you must escape that hyphen
523with a backslash.
8a118206
RGS
524
525Examples:
526
527 [a-z] # Matches a character that is a lower case ASCII letter.
c1c4ae3a
KW
528 [a-fz] # Matches any letter between 'a' and 'f' (inclusive) or
529 # the letter 'z'.
8a118206
RGS
530 [-z] # Matches either a hyphen ('-') or the letter 'z'.
531 [a-f-m] # Matches any letter between 'a' and 'f' (inclusive), the
532 # hyphen ('-'), or the letter 'm'.
533 ['-?] # Matches any of the characters '()*+,-./0123456789:;<=>?
534 # (But not on an EBCDIC platform).
535
536
537=head3 Negation
538
539It is also possible to instead list the characters you do not want to
540match. You can do so by using a caret (C<^>) as the first character in the
b6538e4f 541character class. For instance, C<[^a-z]> matches any character that is not a
e2cfb18c
KW
542lowercase ASCII letter, which therefore includes more than a million
543Unicode code points. The class is said to be "negated" or "inverted".
8a118206
RGS
544
545This syntax make the caret a special character inside a bracketed character
546class, but only if it is the first character of the class. So if you want
82206b5e 547the caret as one of the characters to match, either escape the caret or
e2cfb18c 548else don't list it first.
8a118206 549
1f59b283
KW
550In inverted bracketed character classes, Perl ignores the Unicode rules
551that normally say that a given character matches a sequence of multiple
552characters under caseless C</i> matching, which otherwise could be
553highly confusing:
554
555 "ss" =~ /^[^\xDF]+$/ui;
556
557This should match any sequences of characters that aren't C<\xDF> nor
558what C<\xDF> matches under C</i>. C<"s"> isn't C<\xDF>, but Unicode
559says that C<"ss"> is what C<\xDF> matches under C</i>. So which one
560"wins"? Do you fail the match because the string has C<ss> or accept it
561because it has an C<s> followed by another C<s>?
562
8a118206
RGS
563Examples:
564
565 "e" =~ /[^aeiou]/ # No match, the 'e' is listed.
566 "x" =~ /[^aeiou]/ # Match, as 'x' isn't a lowercase vowel.
567 "^" =~ /[^^]/ # No match, matches anything that isn't a caret.
568 "^" =~ /[x^]/ # Match, caret is not special here.
569
570=head3 Backslash Sequences
571
ea449505 572You can put any backslash sequence character class (with the exception of
765fa144 573C<\N> and C<\R>) inside a bracketed character class, and it will act just
b6538e4f
TC
574as if you had put all characters matched by the backslash sequence inside the
575character class. For instance, C<[a-f\d]> matches any decimal digit, or any
6b83a163
KW
576of the lowercase letters between 'a' and 'f' inclusive.
577
578C<\N> within a bracketed character class must be of the forms C<\N{I<name>}>
765fa144 579or C<\N{U+I<hex char>}>, and NOT be the form that matches non-newlines,
6b83a163
KW
580for the same reason that a dot C<.> inside a bracketed character class loses
581its special meaning: it matches nearly anything, which generally isn't what you
582want to happen.
df225385 583
8a118206
RGS
584
585Examples:
586
587 /[\p{Thai}\d]/ # Matches a character that is either a Thai
588 # character, or a digit.
589 /[^\p{Arabic}()]/ # Matches a character that is neither an Arabic
590 # character, nor a parenthesis.
591
592Backslash sequence character classes cannot form one of the endpoints
6b83a163
KW
593of a range. Thus, you can't say:
594
595 /[\p{Thai}-\d]/ # Wrong!
8a118206 596
6b83a163 597=head3 POSIX Character Classes
ea449505 598X<character class> X<\p> X<\p{}>
ea449505
KW
599X<alpha> X<alnum> X<ascii> X<blank> X<cntrl> X<digit> X<graph>
600X<lower> X<print> X<punct> X<space> X<upper> X<word> X<xdigit>
8a118206 601
6b83a163
KW
602POSIX character classes have the form C<[:class:]>, where I<class> is
603name, and the C<[:> and C<:]> delimiters. POSIX character classes only appear
8a118206 604I<inside> bracketed character classes, and are a convenient and descriptive
82206b5e 605way of listing a group of characters.
6b83a163
KW
606
607Be careful about the syntax,
8a118206
RGS
608
609 # Correct:
610 $string =~ /[[:alpha:]]/
611
612 # Incorrect (will warn):
613 $string =~ /[:alpha:]/
614
615The latter pattern would be a character class consisting of a colon,
616and the letters C<a>, C<l>, C<p> and C<h>.
82206b5e 617POSIX character classes can be part of a larger bracketed character class.
b6538e4f 618For example,
ea449505
KW
619
620 [01[:alpha:]%]
621
622is valid and matches '0', '1', any alphabetic character, and the percent sign.
8a118206
RGS
623
624Perl recognizes the following POSIX character classes:
625
ea449505 626 alpha Any alphabetical character ("[A-Za-z]").
b6538e4f 627 alnum Any alphanumeric character. ("[A-Za-z0-9]")
ea449505 628 ascii Any character in the ASCII character set.
ea8b8ad2 629 blank A GNU extension, equal to a space or a horizontal tab ("\t").
ea449505
KW
630 cntrl Any control character. See Note [2] below.
631 digit Any decimal digit ("[0-9]"), equivalent to "\d".
632 graph Any printable character, excluding a space. See Note [3] below.
633 lower Any lowercase character ("[a-z]").
634 print Any printable character, including a space. See Note [4] below.
c1c4ae3a 635 punct Any graphical character excluding "word" characters. Note [5].
ea449505
KW
636 space Any whitespace character. "\s" plus the vertical tab ("\cK").
637 upper Any uppercase character ("[A-Z]").
638 word A Perl extension ("[A-Za-z0-9_]"), equivalent to "\w".
639 xdigit Any hexadecimal digit ("[0-9a-fA-F]").
640
641Most POSIX character classes have two Unicode-style C<\p> property
642counterparts. (They are not official Unicode properties, but Perl extensions
643derived from official Unicode properties.) The table below shows the relation
644between POSIX character classes and these counterparts.
645
646One counterpart, in the column labelled "ASCII-range Unicode" in
b6538e4f 647the table, matches only characters in the ASCII character set.
ea449505
KW
648
649The other counterpart, in the column labelled "Full-range Unicode", matches any
650appropriate characters in the full Unicode character set. For example,
b6538e4f 651C<\p{Alpha}> matches not just the ASCII alphabetic characters, but any
82206b5e 652character in the entire Unicode character set considered alphabetic.
b6538e4f 653The column labelled "backslash sequence" is a (short) synonym for
cbc24f92 654the Full-range Unicode form.
ea449505 655
cbc24f92
KW
656 [[:...:]] ASCII-range Full-range backslash Note
657 Unicode Unicode sequence
ea449505 658 -----------------------------------------------------
cbc24f92
KW
659 alpha \p{PosixAlpha} \p{XPosixAlpha}
660 alnum \p{PosixAlnum} \p{XPosixAlnum}
82206b5e 661 ascii \p{ASCII}
cbc24f92
KW
662 blank \p{PosixBlank} \p{XPosixBlank} \h [1]
663 or \p{HorizSpace} [1]
664 cntrl \p{PosixCntrl} \p{XPosixCntrl} [2]
665 digit \p{PosixDigit} \p{XPosixDigit} \d
666 graph \p{PosixGraph} \p{XPosixGraph} [3]
667 lower \p{PosixLower} \p{XPosixLower}
668 print \p{PosixPrint} \p{XPosixPrint} [4]
669 punct \p{PosixPunct} \p{XPosixPunct} [5]
670 \p{PerlSpace} \p{XPerlSpace} \s [6]
671 space \p{PosixSpace} \p{XPosixSpace} [6]
672 upper \p{PosixUpper} \p{XPosixUpper}
673 word \p{PosixWord} \p{XPosixWord} \w
82206b5e 674 xdigit \p{PosixXDigit} \p{XPosixXDigit}
8a118206
RGS
675
676=over 4
677
ea449505
KW
678=item [1]
679
680C<\p{Blank}> and C<\p{HorizSpace}> are synonyms.
681
682=item [2]
8a118206 683
ea449505 684Control characters don't produce output as such, but instead usually control
b6538e4f 685the terminal somehow: for example, newline and backspace are control characters.
82206b5e 686In the ASCII range, characters whose code points are between 0 and 31 inclusive,
ea449505 687plus 127 (C<DEL>) are control characters.
8a118206 688
c1c4ae3a
KW
689On EBCDIC platforms, it is likely that the code page will define C<[[:cntrl:]]>
690to be the EBCDIC equivalents of the ASCII controls, plus the controls
82206b5e 691that in Unicode have code pointss from 128 through 159.
ea449505
KW
692
693=item [3]
8a118206
RGS
694
695Any character that is I<graphical>, that is, visible. This class consists
b6538e4f 696of all alphanumeric characters and all punctuation characters.
8a118206 697
ea449505 698=item [4]
8a118206 699
b6538e4f
TC
700All printable characters, which is the set of all graphical characters
701plus those whitespace characters which are not also controls.
ea449505 702
b6dac59a 703=item [5]
ea449505 704
b6538e4f 705C<\p{PosixPunct}> and C<[[:punct:]]> in the ASCII range match all
ea449505
KW
706non-controls, non-alphanumeric, non-space characters:
707C<[-!"#$%&'()*+,./:;<=E<gt>?@[\\\]^_`{|}~]> (although if a locale is in effect,
708it could alter the behavior of C<[[:punct:]]>).
709
cbc24f92
KW
710The similarly named property, C<\p{Punct}>, matches a somewhat different
711set in the ASCII range, namely
6c5a041f
KW
712C<[-!"#%&'()*,./:;?@[\\\]_{}]>. That is, it is missing C<[$+E<lt>=E<gt>^`|~]>.
713This is because Unicode splits what POSIX considers to be punctuation into two
714categories, Punctuation and Symbols.
715
e2cfb18c 716C<\p{XPosixPunct}> and (under Unicode rules) C<[[:punct:]]>, match what
765fa144
KW
717C<\p{PosixPunct}> matches in the ASCII range, plus what C<\p{Punct}>
718matches. This is different than strictly matching according to
719C<\p{Punct}>. Another way to say it is that
82206b5e
KW
720if Unicode rules are in effect, C<[[:punct:]]> matches all characters
721that Unicode considers punctuation, plus all ASCII-range characters that
722Unicode considers symbols.
8a118206 723
ea449505 724=item [6]
8a118206 725
82206b5e
KW
726C<\p{SpacePerl}> and C<\p{Space}> differ only in that in non-locale
727matching, C<\p{Space}> additionally
ea449505 728matches the vertical tab, C<\cK>. Same for the two ASCII-only range forms.
8a118206
RGS
729
730=back
731
ab6199be
KW
732There are various other synonyms that can be used besides the names
733listed in the table. For example, C<\p{PosixAlpha}> can be written as
734C<\p{Alpha}>. All are listed in
735L<perluniprops/Properties accessible through \p{} and \P{}>,
736plus all characters matched by each ASCII-range property.
737
738Both the C<\p> counterparts always assume Unicode rules are in effect.
739On ASCII platforms, this means they assume that the code points from 128
740to 255 are Latin-1, and that means that using them under locale rules is
741unwise unless the locale is guaranteed to be Latin-1 or UTF-8. In contrast, the
742POSIX character classes are useful under locale rules. They are
743affected by the actual rules in effect, as follows:
744
745=over
746
747=item If the C</a> modifier, is in effect ...
748
749Each of the POSIX classes matches exactly the same as their ASCII-range
750counterparts.
751
752=item otherwise ...
753
754=over
755
756=item For code points above 255 ...
757
758The POSIX class matches the same as its Full-range counterpart.
759
760=item For code points below 256 ...
761
762=over
763
764=item if locale rules are in effect ...
765
766The POSIX class matches according to the locale.
767
768=item if Unicode rules are in effect or if on an EBCDIC platform ...
769
770The POSIX class matches the same as the Full-range counterpart.
771
772=item otherwise ...
773
774The POSIX class matches the same as the ASCII range counterpart.
775
776=back
777
778=back
779
780=back
781
782Which rules apply are determined as described in
783L<perlre/Which character set modifier is in effect?>.
784
785It is proposed to change this behavior in a future release of Perl so that
786whether or not Unicode rules are in effect would not change the
787behavior: Outside of locale or an EBCDIC code page, the POSIX classes
788would behave like their ASCII-range counterparts. If you wish to
789comment on this proposal, send email to C<perl5-porters@perl.org>.
cbc24f92 790
1f59b283 791=head4 Negation of POSIX character classes
ea449505 792X<character class, negation>
8a118206
RGS
793
794A Perl extension to the POSIX character class is the ability to
795negate it. This is done by prefixing the class name with a caret (C<^>).
796Some examples:
797
ea449505
KW
798 POSIX ASCII-range Full-range backslash
799 Unicode Unicode sequence
800 -----------------------------------------------------
cbc24f92
KW
801 [[:^digit:]] \P{PosixDigit} \P{XPosixDigit} \D
802 [[:^space:]] \P{PosixSpace} \P{XPosixSpace}
803 \P{PerlSpace} \P{XPerlSpace} \S
804 [[:^word:]] \P{PerlWord} \P{XPosixWord} \W
805
765fa144 806The backslash sequence can mean either ASCII- or Full-range Unicode,
82206b5e 807depending on various factors as described in L<perlre/Which character set modifier is in effect?>.
8a118206
RGS
808
809=head4 [= =] and [. .]
810
b6538e4f 811Perl recognizes the POSIX character classes C<[=class=]> and
82206b5e 812C<[.class.]>, but does not (yet?) support them. Any attempt to use
b6538e4f 813either construct raises an exception.
8a118206
RGS
814
815=head4 Examples
816
817 /[[:digit:]]/ # Matches a character that is a digit.
818 /[01[:lower:]]/ # Matches a character that is either a
819 # lowercase letter, or '0' or '1'.
c1c4ae3a
KW
820 /[[:digit:][:^xdigit:]]/ # Matches a character that can be anything
821 # except the letters 'a' to 'f'. This is
822 # because the main character class is composed
823 # of two POSIX character classes that are ORed
824 # together, one that matches any digit, and
825 # the other that matches anything that isn't a
826 # hex digit. The result matches all
827 # characters except the letters 'a' to 'f' and
828 # 'A' to 'F'.