4 perlrecharclass - Perl Regular Expression Character Classes
8 The top level documentation about Perl regular expressions
11 This manual page discusses the syntax and use of character
12 classes in Perl regular expressions.
14 A character class is a way of denoting a set of characters
15 in such a way that one character of the set is matched.
16 It's important to remember that: matching a character class
17 consumes exactly one character in the source string. (The source
18 string is the string the regular expression is matched against.)
20 There are three types of character classes in Perl regular
21 expressions: the dot, backslash sequences, and the form enclosed in square
22 brackets. Keep in mind, though, that often the term "character class" is used
23 to mean just the bracketed form. Certainly, most Perl documentation does that.
27 The dot (or period), C<.> is probably the most used, and certainly
28 the most well-known character class. By default, a dot matches any
29 character, except for the newline. That default can be changed to
30 add matching the newline by using the I<single line> modifier: either
31 for the entire regular expression with the C</s> modifier, or
32 locally with C<(?s)>. (The experimental C<\N> backslash sequence, described
33 below, matches any character except newline without regard to the
34 I<single line> modifier.)
36 Here are some examples:
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)
46 =head2 Backslash sequences
47 X<\w> X<\W> X<\s> X<\S> X<\d> X<\D> X<\p> X<\P>
48 X<\N> X<\v> X<\V> X<\h> X<\H>
51 A backslash sequence is a sequence of characters, the first one of which is a
52 backslash. Perl ascribes special meaning to many such sequences, and some of
53 these are character classes. That is, they match a single character each,
54 provided that the character belongs to the specific set of characters defined
57 Here's a list of the backslash sequences that are character classes. They
58 are discussed in more detail below. (For the backslash sequences that aren't
59 character classes, see L<perlrebackslash>.)
61 \d Match a decimal digit character.
62 \D Match a non-decimal-digit character.
63 \w Match a "word" character.
64 \W Match a non-"word" character.
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.
69 \v Match a vertical whitespace character.
70 \V Match a character that isn't vertical whitespace.
71 \N Match a character that isn't a newline. Experimental.
72 \pP, \p{Prop} Match a character that has the given Unicode property.
73 \PP, \P{Prop} Match a character that doesn't have the Unicode property
77 C<\N> is new in 5.12, and is experimental. It, like the dot, matches any
78 character that is not a newline. The difference is that C<\N> is not influenced
79 by the I<single line> regular expression modifier (see L</The dot> above). Note
80 that the form C<\N{...}> may mean something completely different. When the
81 C<{...}> is a L<quantifier|perlre/Quantifiers>, it means to match a non-newline
82 character that many times. For example, C<\N{3}> means to match 3
83 non-newlines; C<\N{5,}> means to match 5 or more non-newlines. But if C<{...}>
84 is not a legal quantifier, it is presumed to be a named character. See
85 L<charnames> for those. For example, none of C<\N{COLON}>, C<\N{4F}>, and
86 C<\N{F4}> contain legal quantifiers, so Perl will try to find characters whose
87 names are respectively C<COLON>, C<4F>, and C<F4>.
91 C<\d> matches a single character considered to be a decimal I<digit>.
92 If the C</a> regular expression modifier is in effect, it matches [0-9].
94 matches anything that is matched by C<\p{Digit}>, which includes [0-9].
95 (An unlikely possible exception is that under locale matching rules, the
96 current locale might not have [0-9] matched by C<\d>, and/or might match
97 other characters whose code point is less than 256. Such a locale
98 definition would be in violation of the C language standard, but Perl
99 doesn't currently assume anything in regard to this.)
101 What this means is that unless the C</a> modifier is in effect C<\d> not
102 only matches the digits '0' - '9', but also Arabic, Devanagari, and
103 digits from other languages. This may cause some confusion, and some
106 Some digits that C<\d> matches look like some of the [0-9] ones, but
107 have different values. For example, BENGALI DIGIT FOUR (U+09EA) looks
108 very much like an ASCII DIGIT EIGHT (U+0038). An application that
109 is expecting only the ASCII digits might be misled, or if the match is
110 C<\d+>, the matched string might contain a mixture of digits from
111 different writing systems that look like they signify a number different
112 than they actually do. L<Unicode::UCD/num()> can
114 calculate the value, returning C<undef> if the input string contains
117 What C<\p{Digit}> means (and hence C<\d> except under the C</a>
118 modifier) is C<\p{General_Category=Decimal_Number}>, or synonymously,
119 C<\p{General_Category=Digit}>. Starting with Unicode version 4.1, this
120 is the same set of characters matched by C<\p{Numeric_Type=Decimal}>.
121 But Unicode also has a different property with a similar name,
122 C<\p{Numeric_Type=Digit}>, which matches a completely different set of
123 characters. These characters are things such as C<CIRCLED DIGIT ONE>
124 or subscripts, or are from writing systems that lack all ten digits.
126 The design intent is for C<\d> to exactly match the set of characters
127 that can safely be used with "normal" big-endian positional decimal
128 syntax, where, for example 123 means one 'hundred', plus two 'tens',
129 plus three 'ones'. This positional notation does not necessarily apply
130 to characters that match the other type of "digit",
131 C<\p{Numeric_Type=Digit}>, and so C<\d> doesn't match them.
133 The Tamil digits (U+0BE6 - U+0BEF) can also legally be
134 used in old-style Tamil numbers in which they would appear no more than
135 one in a row, separated by characters that mean "times 10", "times 100",
136 etc. (See L<http://www.unicode.org/notes/tn21>.)
138 Any character not matched by C<\d> is matched by C<\D>.
140 =head3 Word characters
142 A C<\w> matches a single alphanumeric character (an alphabetic character, or a
143 decimal digit) or a connecting punctuation character, such as an
144 underscore ("_"). It does not match a whole word. To match a whole
145 word, use C<\w+>. This isn't the same thing as matching an English word, but
146 in the ASCII range it is the same as a string of Perl-identifier
151 =item If the C</a> modifier is in effect ...
153 C<\w> matches the 63 characters [a-zA-Z0-9_].
159 =item For code points above 255 ...
161 C<\w> matches the same as C<\p{Word}> matches in this range. That is,
162 it matches Thai letters, Greek letters, etc. This includes connector
163 punctuation (like the underscore) which connect two words together, or
164 diacritics, such as a C<COMBINING TILDE> and the modifier letters, which
165 are generally used to add auxiliary markings to letters.
167 =item For code points below 256 ...
171 =item if locale rules are in effect ...
173 C<\w> matches the platform's native underscore character plus whatever
174 the locale considers to be alphanumeric.
176 =item if Unicode rules are in effect or if on an EBCDIC platform ...
178 C<\w> matches exactly what C<\p{Word}> matches.
182 C<\w> matches [a-zA-Z0-9_].
190 Which rules apply are determined as described in L<perlre/Which character set modifier is in effect?>.
192 There are a number of security issues with the full Unicode list of word
193 characters. See L<http://unicode.org/reports/tr36>.
195 Also, for a somewhat finer-grained set of characters that are in programming
196 language identifiers beyond the ASCII range, you may wish to instead use the
197 more customized L</Unicode Properties>, C<\p{ID_Start}>,
198 C<\p{ID_Continue}>, C<\p{XID_Start}>, and C<\p{XID_Continue}>. See
199 L<http://unicode.org/reports/tr31>.
201 Any character not matched by C<\w> is matched by C<\W>.
205 C<\s> matches any single character considered whitespace.
209 =item If the C</a> modifier is in effect ...
211 C<\s> matches the 5 characters [\t\n\f\r ]; that is, the horizontal tab,
212 the newline, the form feed, the carriage return, and the space. (Note
213 that it doesn't match the vertical tab, C<\cK> on ASCII platforms.)
219 =item For code points above 255 ...
221 C<\s> matches exactly the code points above 255 shown with an "s" column
224 =item For code points below 256 ...
228 =item if locale rules are in effect ...
230 C<\s> matches whatever the locale considers to be whitespace. Note that
231 this is likely to include the vertical space, unlike non-locale C<\s>
234 =item if Unicode rules are in effect or if on an EBCDIC platform ...
236 C<\s> matches exactly the characters shown with an "s" column in the
241 C<\s> matches [\t\n\f\r ].
242 Note that this list doesn't include the non-breaking space.
250 Which rules apply are determined as described in L<perlre/Which character set modifier is in effect?>.
252 Any character not matched by C<\s> is matched by C<\S>.
254 C<\h> matches any character considered horizontal whitespace;
255 this includes the space and tab characters and several others
256 listed in the table below. C<\H> matches any character
257 not considered horizontal whitespace.
259 C<\v> matches any character considered vertical whitespace;
260 this includes the carriage return and line feed characters (newline)
261 plus several other characters, all listed in the table below.
262 C<\V> matches any character not considered vertical whitespace.
264 C<\R> matches anything that can be considered a newline under Unicode
265 rules. It's not a character class, as it can match a multi-character
266 sequence. Therefore, it cannot be used inside a bracketed character
267 class; use C<\v> instead (vertical whitespace).
268 Details are discussed in L<perlrebackslash>.
270 Note that unlike C<\s> (and C<\d> and C<\w>), C<\h> and C<\v> always match
271 the same characters, without regard to other factors, such as whether the
272 source string is in UTF-8 format.
274 One might think that C<\s> is equivalent to C<[\h\v]>. This is not true.
275 The difference is that the vertical tab (C<"\x0b">) is not matched by
276 C<\s>; it is however considered vertical whitespace.
278 The following table is a complete listing of characters matched by
279 C<\s>, C<\h> and C<\v> as of Unicode 6.0.
281 The first column gives the Unicode code point of the character (in hex format),
282 the second column gives the (Unicode) name. The third column indicates
283 by which class(es) the character is matched (assuming no locale or EBCDIC code
284 page is in effect that changes the C<\s> matching).
286 0x0009 CHARACTER TABULATION h s
287 0x000a LINE FEED (LF) vs
288 0x000b LINE TABULATION v
289 0x000c FORM FEED (FF) vs
290 0x000d CARRIAGE RETURN (CR) vs
292 0x0085 NEXT LINE (NEL) vs [1]
293 0x00a0 NO-BREAK SPACE h s [1]
294 0x1680 OGHAM SPACE MARK h s
295 0x180e MONGOLIAN VOWEL SEPARATOR h s
300 0x2004 THREE-PER-EM SPACE h s
301 0x2005 FOUR-PER-EM SPACE h s
302 0x2006 SIX-PER-EM SPACE h s
303 0x2007 FIGURE SPACE h s
304 0x2008 PUNCTUATION SPACE h s
305 0x2009 THIN SPACE h s
306 0x200a HAIR SPACE h s
307 0x2028 LINE SEPARATOR vs
308 0x2029 PARAGRAPH SEPARATOR vs
309 0x202f NARROW NO-BREAK SPACE h s
310 0x205f MEDIUM MATHEMATICAL SPACE h s
311 0x3000 IDEOGRAPHIC SPACE h s
317 NEXT LINE and NO-BREAK SPACE may or may not match C<\s> depending
318 on the rules in effect. See
319 L<the beginning of this section|/Whitespace>.
323 =head3 Unicode Properties
325 C<\pP> and C<\p{Prop}> are character classes to match characters that fit given
326 Unicode properties. One letter property names can be used in the C<\pP> form,
327 with the property name following the C<\p>, otherwise, braces are required.
328 When using braces, there is a single form, which is just the property name
329 enclosed in the braces, and a compound form which looks like C<\p{name=value}>,
330 which means to match if the property "name" for the character has that particular
332 For instance, a match for a number can be written as C</\pN/> or as
333 C</\p{Number}/>, or as C</\p{Number=True}/>.
334 Lowercase letters are matched by the property I<Lowercase_Letter> which
335 has the short form I<Ll>. They need the braces, so are written as C</\p{Ll}/> or
336 C</\p{Lowercase_Letter}/>, or C</\p{General_Category=Lowercase_Letter}/>
337 (the underscores are optional).
338 C</\pLl/> is valid, but means something different.
339 It matches a two character string: a letter (Unicode property C<\pL>),
340 followed by a lowercase C<l>.
342 If neither the C</a> modifier nor locale rules are in effect, the use of
343 a Unicode property will force the regular expression into using Unicode
346 Note that almost all properties are immune to case-insensitive matching.
347 That is, adding a C</i> regular expression modifier does not change what
348 they match. There are two sets that are affected. The first set is
351 and C<Titlecase_Letter>,
352 all of which match C<Cased_Letter> under C</i> matching.
357 all of which match C<Cased> under C</i> matching.
358 (The difference between these sets is that some things, such as Roman
359 numerals, come in both upper and lower case, so they are C<Cased>, but
360 aren't considered to be letters, so they aren't C<Cased_Letter>s. They're
361 actually C<Letter_Number>s.)
362 This set also includes its subsets C<PosixUpper> and C<PosixLower>, both
363 of which under C</i> match C<PosixAlpha>.
365 For more details on Unicode properties, see L<perlunicode/Unicode
366 Character Properties>; for a
367 complete list of possible properties, see
368 L<perluniprops/Properties accessible through \p{} and \P{}>,
369 which notes all forms that have C</i> differences.
370 It is also possible to define your own properties. This is discussed in
371 L<perlunicode/User-Defined Character Properties>.
373 Unicode properties are defined (surprise!) only on Unicode code points.
374 A warning is raised and all matches fail on non-Unicode code points
375 (those above the legal Unicode maximum of 0x10FFFF). This can be
378 chr(0x110000) =~ \p{ASCII_Hex_Digit=True} # Fails.
379 chr(0x110000) =~ \p{ASCII_Hex_Digit=False} # Also fails!
381 Even though these two matches might be thought of as complements, they
382 are so only on Unicode code points.
386 "a" =~ /\w/ # Match, "a" is a 'word' character.
387 "7" =~ /\w/ # Match, "7" is a 'word' character as well.
388 "a" =~ /\d/ # No match, "a" isn't a digit.
389 "7" =~ /\d/ # Match, "7" is a digit.
390 " " =~ /\s/ # Match, a space is whitespace.
391 "a" =~ /\D/ # Match, "a" is a non-digit.
392 "7" =~ /\D/ # No match, "7" is not a non-digit.
393 " " =~ /\S/ # No match, a space is not non-whitespace.
395 " " =~ /\h/ # Match, space is horizontal whitespace.
396 " " =~ /\v/ # No match, space is not vertical whitespace.
397 "\r" =~ /\v/ # Match, a return is vertical whitespace.
399 "a" =~ /\pL/ # Match, "a" is a letter.
400 "a" =~ /\p{Lu}/ # No match, /\p{Lu}/ matches upper case letters.
402 "\x{0e0b}" =~ /\p{Thai}/ # Match, \x{0e0b} is the character
403 # 'THAI CHARACTER SO SO', and that's in
404 # Thai Unicode class.
405 "a" =~ /\P{Lao}/ # Match, as "a" is not a Laotian character.
407 It is worth emphasizing that C<\d>, C<\w>, etc, match single characters, not
408 complete numbers or words. To match a number (that consists of digits),
409 use C<\d+>; to match a word, use C<\w+>. But be aware of the security
410 considerations in doing so, as mentioned above.
412 =head2 Bracketed Character Classes
414 The third form of character class you can use in Perl regular expressions
415 is the bracketed character class. In its simplest form, it lists the characters
416 that may be matched, surrounded by square brackets, like this: C<[aeiou]>.
417 This matches one of C<a>, C<e>, C<i>, C<o> or C<u>. Like the other
418 character classes, exactly one character is matched.* To match
419 a longer string consisting of characters mentioned in the character
420 class, follow the character class with a L<quantifier|perlre/Quantifiers>. For
421 instance, C<[aeiou]+> matches one or more lowercase English vowels.
423 Repeating a character in a character class has no
424 effect; it's considered to be in the set only once.
428 "e" =~ /[aeiou]/ # Match, as "e" is listed in the class.
429 "p" =~ /[aeiou]/ # No match, "p" is not listed in the class.
430 "ae" =~ /^[aeiou]$/ # No match, a character class only matches
431 # a single character.
432 "ae" =~ /^[aeiou]+$/ # Match, due to the quantifier.
436 * There is an exception to a bracketed character class matching a
437 single character only. When the class is to match caselessly under C</i>
438 matching rules, and a character inside the class matches a
439 multiple-character sequence caselessly under Unicode rules, the class
440 (when not L<inverted|/Negation>) will also match that sequence. For
441 example, Unicode says that the letter C<LATIN SMALL LETTER SHARP S>
442 should match the sequence C<ss> under C</i> rules. Thus,
444 'ss' =~ /\A\N{LATIN SMALL LETTER SHARP S}\z/i # Matches
445 'ss' =~ /\A[aeioust\N{LATIN SMALL LETTER SHARP S}]\z/i # Matches
447 =head3 Special Characters Inside a Bracketed Character Class
449 Most characters that are meta characters in regular expressions (that
450 is, characters that carry a special meaning like C<.>, C<*>, or C<(>) lose
451 their special meaning and can be used inside a character class without
452 the need to escape them. For instance, C<[()]> matches either an opening
453 parenthesis, or a closing parenthesis, and the parens inside the character
454 class don't group or capture.
456 Characters that may carry a special meaning inside a character class are:
457 C<\>, C<^>, C<->, C<[> and C<]>, and are discussed below. They can be
458 escaped with a backslash, although this is sometimes not needed, in which
459 case the backslash may be omitted.
461 The sequence C<\b> is special inside a bracketed character class. While
462 outside the character class, C<\b> is an assertion indicating a point
463 that does not have either two word characters or two non-word characters
464 on either side, inside a bracketed character class, C<\b> matches a
474 C<\N{U+I<hex char>}>,
479 are also special and have the same meanings as they do outside a
480 bracketed character class. (However, inside a bracketed character
481 class, if C<\N{I<NAME>}> expands to a sequence of characters, only the first
482 one in the sequence is used, with a warning.)
484 Also, a backslash followed by two or three octal digits is considered an octal
487 A C<[> is not special inside a character class, unless it's the start of a
488 POSIX character class (see L</POSIX Character Classes> below). It normally does
491 A C<]> is normally either the end of a POSIX character class (see
492 L</POSIX Character Classes> below), or it signals the end of the bracketed
493 character class. If you want to include a C<]> in the set of characters, you
494 must generally escape it.
496 However, if the C<]> is the I<first> (or the second if the first
497 character is a caret) character of a bracketed character class, it
498 does not denote the end of the class (as you cannot have an empty class)
499 and is considered part of the set of characters that can be matched without
504 "+" =~ /[+?*]/ # Match, "+" in a character class is not special.
505 "\cH" =~ /[\b]/ # Match, \b inside in a character class
506 # is equivalent to a backspace.
507 "]" =~ /[][]/ # Match, as the character class contains.
509 "[]" =~ /[[]]/ # Match, the pattern contains a character class
510 # containing just ], and the character class is
513 =head3 Character Ranges
515 It is not uncommon to want to match a range of characters. Luckily, instead
516 of listing all characters in the range, one may use the hyphen (C<->).
517 If inside a bracketed character class you have two characters separated
518 by a hyphen, it's treated as if all characters between the two were in
519 the class. For instance, C<[0-9]> matches any ASCII digit, and C<[a-m]>
520 matches any lowercase letter from the first half of the ASCII alphabet.
522 Note that the two characters on either side of the hyphen are not
523 necessarily both letters or both digits. Any character is possible,
524 although not advisable. C<['-?]> contains a range of characters, but
525 most people will not know which characters that means. Furthermore,
526 such ranges may lead to portability problems if the code has to run on
527 a platform that uses a different character set, such as EBCDIC.
529 If a hyphen in a character class cannot syntactically be part of a range, for
530 instance because it is the first or the last character of the character class,
531 or if it immediately follows a range, the hyphen isn't special, and so is
532 considered a character to be matched literally. If you want a hyphen in
533 your set of characters to be matched and its position in the class is such
534 that it could be considered part of a range, you must escape that hyphen
539 [a-z] # Matches a character that is a lower case ASCII letter.
540 [a-fz] # Matches any letter between 'a' and 'f' (inclusive) or
542 [-z] # Matches either a hyphen ('-') or the letter 'z'.
543 [a-f-m] # Matches any letter between 'a' and 'f' (inclusive), the
544 # hyphen ('-'), or the letter 'm'.
545 ['-?] # Matches any of the characters '()*+,-./0123456789:;<=>?
546 # (But not on an EBCDIC platform).
551 It is also possible to instead list the characters you do not want to
552 match. You can do so by using a caret (C<^>) as the first character in the
553 character class. For instance, C<[^a-z]> matches any character that is not a
554 lowercase ASCII letter, which therefore includes more than a million
555 Unicode code points. The class is said to be "negated" or "inverted".
557 This syntax make the caret a special character inside a bracketed character
558 class, but only if it is the first character of the class. So if you want
559 the caret as one of the characters to match, either escape the caret or
560 else don't list it first.
562 In inverted bracketed character classes, Perl ignores the Unicode rules
563 that normally say that certain characters should match a sequence of
564 multiple characters under caseless C</i> matching. Following those
565 rules could lead to highly confusing situations:
567 "ss" =~ /^[^\xDF]+$/ui; # Matches!
569 This should match any sequences of characters that aren't C<\xDF> nor
570 what C<\xDF> matches under C</i>. C<"s"> isn't C<\xDF>, but Unicode
571 says that C<"ss"> is what C<\xDF> matches under C</i>. So which one
572 "wins"? Do you fail the match because the string has C<ss> or accept it
573 because it has an C<s> followed by another C<s>? Perl has chosen the
578 "e" =~ /[^aeiou]/ # No match, the 'e' is listed.
579 "x" =~ /[^aeiou]/ # Match, as 'x' isn't a lowercase vowel.
580 "^" =~ /[^^]/ # No match, matches anything that isn't a caret.
581 "^" =~ /[x^]/ # Match, caret is not special here.
583 =head3 Backslash Sequences
585 You can put any backslash sequence character class (with the exception of
586 C<\N> and C<\R>) inside a bracketed character class, and it will act just
587 as if you had put all characters matched by the backslash sequence inside the
588 character class. For instance, C<[a-f\d]> matches any decimal digit, or any
589 of the lowercase letters between 'a' and 'f' inclusive.
591 C<\N> within a bracketed character class must be of the forms C<\N{I<name>}>
592 or C<\N{U+I<hex char>}>, and NOT be the form that matches non-newlines,
593 for the same reason that a dot C<.> inside a bracketed character class loses
594 its special meaning: it matches nearly anything, which generally isn't what you
600 /[\p{Thai}\d]/ # Matches a character that is either a Thai
601 # character, or a digit.
602 /[^\p{Arabic}()]/ # Matches a character that is neither an Arabic
603 # character, nor a parenthesis.
605 Backslash sequence character classes cannot form one of the endpoints
606 of a range. Thus, you can't say:
608 /[\p{Thai}-\d]/ # Wrong!
610 =head3 POSIX Character Classes
611 X<character class> X<\p> X<\p{}>
612 X<alpha> X<alnum> X<ascii> X<blank> X<cntrl> X<digit> X<graph>
613 X<lower> X<print> X<punct> X<space> X<upper> X<word> X<xdigit>
615 POSIX character classes have the form C<[:class:]>, where I<class> is
616 name, and the C<[:> and C<:]> delimiters. POSIX character classes only appear
617 I<inside> bracketed character classes, and are a convenient and descriptive
618 way of listing a group of characters.
620 Be careful about the syntax,
623 $string =~ /[[:alpha:]]/
625 # Incorrect (will warn):
626 $string =~ /[:alpha:]/
628 The latter pattern would be a character class consisting of a colon,
629 and the letters C<a>, C<l>, C<p> and C<h>.
630 POSIX character classes can be part of a larger bracketed character class.
635 is valid and matches '0', '1', any alphabetic character, and the percent sign.
637 Perl recognizes the following POSIX character classes:
639 alpha Any alphabetical character ("[A-Za-z]").
640 alnum Any alphanumeric character. ("[A-Za-z0-9]")
641 ascii Any character in the ASCII character set.
642 blank A GNU extension, equal to a space or a horizontal tab ("\t").
643 cntrl Any control character. See Note [2] below.
644 digit Any decimal digit ("[0-9]"), equivalent to "\d".
645 graph Any printable character, excluding a space. See Note [3] below.
646 lower Any lowercase character ("[a-z]").
647 print Any printable character, including a space. See Note [4] below.
648 punct Any graphical character excluding "word" characters. Note [5].
649 space Any whitespace character. "\s" plus the vertical tab ("\cK").
650 upper Any uppercase character ("[A-Z]").
651 word A Perl extension ("[A-Za-z0-9_]"), equivalent to "\w".
652 xdigit Any hexadecimal digit ("[0-9a-fA-F]").
654 Most POSIX character classes have two Unicode-style C<\p> property
655 counterparts. (They are not official Unicode properties, but Perl extensions
656 derived from official Unicode properties.) The table below shows the relation
657 between POSIX character classes and these counterparts.
659 One counterpart, in the column labelled "ASCII-range Unicode" in
660 the table, matches only characters in the ASCII character set.
662 The other counterpart, in the column labelled "Full-range Unicode", matches any
663 appropriate characters in the full Unicode character set. For example,
664 C<\p{Alpha}> matches not just the ASCII alphabetic characters, but any
665 character in the entire Unicode character set considered alphabetic.
666 An entry in the column labelled "backslash sequence" is a (short)
669 [[:...:]] ASCII-range Full-range backslash Note
670 Unicode Unicode sequence
671 -----------------------------------------------------
672 alpha \p{PosixAlpha} \p{XPosixAlpha}
673 alnum \p{PosixAlnum} \p{XPosixAlnum}
675 blank \p{PosixBlank} \p{XPosixBlank} \h [1]
676 or \p{HorizSpace} [1]
677 cntrl \p{PosixCntrl} \p{XPosixCntrl} [2]
678 digit \p{PosixDigit} \p{XPosixDigit} \d
679 graph \p{PosixGraph} \p{XPosixGraph} [3]
680 lower \p{PosixLower} \p{XPosixLower}
681 print \p{PosixPrint} \p{XPosixPrint} [4]
682 punct \p{PosixPunct} \p{XPosixPunct} [5]
683 \p{PerlSpace} \p{XPerlSpace} \s [6]
684 space \p{PosixSpace} \p{XPosixSpace} [6]
685 upper \p{PosixUpper} \p{XPosixUpper}
686 word \p{PosixWord} \p{XPosixWord} \w
687 xdigit \p{PosixXDigit} \p{XPosixXDigit}
693 C<\p{Blank}> and C<\p{HorizSpace}> are synonyms.
697 Control characters don't produce output as such, but instead usually control
698 the terminal somehow: for example, newline and backspace are control characters.
699 In the ASCII range, characters whose code points are between 0 and 31 inclusive,
700 plus 127 (C<DEL>) are control characters.
702 On EBCDIC platforms, it is likely that the code page will define C<[[:cntrl:]]>
703 to be the EBCDIC equivalents of the ASCII controls, plus the controls
704 that in Unicode have code pointss from 128 through 159.
708 Any character that is I<graphical>, that is, visible. This class consists
709 of all alphanumeric characters and all punctuation characters.
713 All printable characters, which is the set of all graphical characters
714 plus those whitespace characters which are not also controls.
718 C<\p{PosixPunct}> and C<[[:punct:]]> in the ASCII range match all
719 non-controls, non-alphanumeric, non-space characters:
720 C<[-!"#$%&'()*+,./:;<=E<gt>?@[\\\]^_`{|}~]> (although if a locale is in effect,
721 it could alter the behavior of C<[[:punct:]]>).
723 The similarly named property, C<\p{Punct}>, matches a somewhat different
724 set in the ASCII range, namely
725 C<[-!"#%&'()*,./:;?@[\\\]_{}]>. That is, it is missing the nine
726 characters C<[$+E<lt>=E<gt>^`|~]>.
727 This is because Unicode splits what POSIX considers to be punctuation into two
728 categories, Punctuation and Symbols.
730 C<\p{XPosixPunct}> and (under Unicode rules) C<[[:punct:]]>, match what
731 C<\p{PosixPunct}> matches in the ASCII range, plus what C<\p{Punct}>
732 matches. This is different than strictly matching according to
733 C<\p{Punct}>. Another way to say it is that
734 if Unicode rules are in effect, C<[[:punct:]]> matches all characters
735 that Unicode considers punctuation, plus all ASCII-range characters that
736 Unicode considers symbols.
740 C<\p{SpacePerl}> and C<\p{Space}> differ only in that in non-locale
741 matching, C<\p{Space}> additionally
742 matches the vertical tab, C<\cK>. Same for the two ASCII-only range forms.
746 There are various other synonyms that can be used besides the names
747 listed in the table. For example, C<\p{PosixAlpha}> can be written as
748 C<\p{Alpha}>. All are listed in
749 L<perluniprops/Properties accessible through \p{} and \P{}>,
750 plus all characters matched by each ASCII-range property.
752 Both the C<\p> counterparts always assume Unicode rules are in effect.
753 On ASCII platforms, this means they assume that the code points from 128
754 to 255 are Latin-1, and that means that using them under locale rules is
755 unwise unless the locale is guaranteed to be Latin-1 or UTF-8. In contrast, the
756 POSIX character classes are useful under locale rules. They are
757 affected by the actual rules in effect, as follows:
761 =item If the C</a> modifier, is in effect ...
763 Each of the POSIX classes matches exactly the same as their ASCII-range
770 =item For code points above 255 ...
772 The POSIX class matches the same as its Full-range counterpart.
774 =item For code points below 256 ...
778 =item if locale rules are in effect ...
780 The POSIX class matches according to the locale.
782 =item if Unicode rules are in effect or if on an EBCDIC platform ...
784 The POSIX class matches the same as the Full-range counterpart.
788 The POSIX class matches the same as the ASCII range counterpart.
796 Which rules apply are determined as described in
797 L<perlre/Which character set modifier is in effect?>.
799 It is proposed to change this behavior in a future release of Perl so that
800 whether or not Unicode rules are in effect would not change the
801 behavior: Outside of locale or an EBCDIC code page, the POSIX classes
802 would behave like their ASCII-range counterparts. If you wish to
803 comment on this proposal, send email to C<perl5-porters@perl.org>.
805 =head4 Negation of POSIX character classes
806 X<character class, negation>
808 A Perl extension to the POSIX character class is the ability to
809 negate it. This is done by prefixing the class name with a caret (C<^>).
812 POSIX ASCII-range Full-range backslash
813 Unicode Unicode sequence
814 -----------------------------------------------------
815 [[:^digit:]] \P{PosixDigit} \P{XPosixDigit} \D
816 [[:^space:]] \P{PosixSpace} \P{XPosixSpace}
817 \P{PerlSpace} \P{XPerlSpace} \S
818 [[:^word:]] \P{PerlWord} \P{XPosixWord} \W
820 The backslash sequence can mean either ASCII- or Full-range Unicode,
821 depending on various factors as described in L<perlre/Which character set modifier is in effect?>.
823 =head4 [= =] and [. .]
825 Perl recognizes the POSIX character classes C<[=class=]> and
826 C<[.class.]>, but does not (yet?) support them. Any attempt to use
827 either construct raises an exception.
831 /[[:digit:]]/ # Matches a character that is a digit.
832 /[01[:lower:]]/ # Matches a character that is either a
833 # lowercase letter, or '0' or '1'.
834 /[[:digit:][:^xdigit:]]/ # Matches a character that can be anything
835 # except the letters 'a' to 'f'. This is
836 # because the main character class is composed
837 # of two POSIX character classes that are ORed
838 # together, one that matches any digit, and
839 # the other that matches anything that isn't a
840 # hex digit. The result matches all
841 # characters except the letters 'a' to 'f' and