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