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