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