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