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