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