| 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 "PERLREBACKSLASH 1" |
| 58 |
.TH PERLREBACKSLASH 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 |
perlrebackslash \- Perl Regular Expression Backslash Sequences and Escapes |
| 65 |
.SH DESCRIPTION |
| 66 |
.IX Header "DESCRIPTION" |
| 67 |
The top level documentation about Perl regular expressions |
| 68 |
is found in perlre. |
| 69 |
.PP |
| 70 |
This document describes all backslash and escape sequences. After |
| 71 |
explaining the role of the backslash, it lists all the sequences that have |
| 72 |
a special meaning in Perl regular expressions (in alphabetical order), |
| 73 |
then describes each of them. |
| 74 |
.PP |
| 75 |
Most sequences are described in detail in different documents; the primary |
| 76 |
purpose of this document is to have a quick reference guide describing all |
| 77 |
backslash and escape sequences. |
| 78 |
.SS "The backslash" |
| 79 |
.IX Subsection "The backslash" |
| 80 |
In a regular expression, the backslash can perform one of two tasks: |
| 81 |
it either takes away the special meaning of the character following it |
| 82 |
(for instance, \f(CW\*(C`\e|\*(C'\fR matches a vertical bar, it's not an alternation), |
| 83 |
or it is the start of a backslash or escape sequence. |
| 84 |
.PP |
| 85 |
The rules determining what it is are quite simple: if the character |
| 86 |
following the backslash is an ASCII punctuation (non-word) character (that is, |
| 87 |
anything that is not a letter, digit, or underscore), then the backslash just |
| 88 |
takes away any special meaning of the character following it. |
| 89 |
.PP |
| 90 |
If the character following the backslash is an ASCII letter or an ASCII digit, |
| 91 |
then the sequence may be special; if so, it's listed below. A few letters have |
| 92 |
not been used yet, so escaping them with a backslash doesn't change them to be |
| 93 |
special. A future version of Perl may assign a special meaning to them, so if |
| 94 |
you have warnings turned on, Perl issues a warning if you use such a |
| 95 |
sequence. [1]. |
| 96 |
.PP |
| 97 |
It is however guaranteed that backslash or escape sequences never have a |
| 98 |
punctuation character following the backslash, not now, and not in a future |
| 99 |
version of Perl 5. So it is safe to put a backslash in front of a non-word |
| 100 |
character. |
| 101 |
.PP |
| 102 |
Note that the backslash itself is special; if you want to match a backslash, |
| 103 |
you have to escape the backslash with a backslash: \f(CW\*(C`/\e\e/\*(C'\fR matches a single |
| 104 |
backslash. |
| 105 |
.IP [1] 4 |
| 106 |
.IX Item "[1]" |
| 107 |
There is one exception. If you use an alphanumeric character as the |
| 108 |
delimiter of your pattern (which you probably shouldn't do for readability |
| 109 |
reasons), you have to escape the delimiter if you want to match |
| 110 |
it. Perl won't warn then. See also "Gory details of parsing |
| 111 |
quoted constructs" in perlop. |
| 112 |
.SS "All the sequences and escapes" |
| 113 |
.IX Subsection "All the sequences and escapes" |
| 114 |
Those not usable within a bracketed character class (like \f(CW\*(C`[\eda\-z]\*(C'\fR) are marked |
| 115 |
as \f(CW\*(C`Not in [].\*(C'\fR |
| 116 |
.PP |
| 117 |
.Vb 10 |
| 118 |
\& \e000 Octal escape sequence. See also \eo{}. |
| 119 |
\& \e1 Absolute backreference. Not in []. |
| 120 |
\& \ea Alarm or bell. |
| 121 |
\& \eA Beginning of string. Not in []. |
| 122 |
\& \eb{}, \eb Boundary. (\eb is a backspace in []). |
| 123 |
\& \eB{}, \eB Not a boundary. Not in []. |
| 124 |
\& \ecX Control\-X. |
| 125 |
\& \ed Match any digit character. |
| 126 |
\& \eD Match any character that isn\*(Aqt a digit. |
| 127 |
\& \ee Escape character. |
| 128 |
\& \eE Turn off \eQ, \eL and \eU processing. Not in []. |
| 129 |
\& \ef Form feed. |
| 130 |
\& \eF Foldcase till \eE. Not in []. |
| 131 |
\& \eg{}, \eg1 Named, absolute or relative backreference. |
| 132 |
\& Not in []. |
| 133 |
\& \eG Pos assertion. Not in []. |
| 134 |
\& \eh Match any horizontal whitespace character. |
| 135 |
\& \eH Match any character that isn\*(Aqt horizontal whitespace. |
| 136 |
\& \ek{}, \ek<>, \ek\*(Aq\*(Aq Named backreference. Not in []. |
| 137 |
\& \eK Keep the stuff left of \eK. Not in []. |
| 138 |
\& \el Lowercase next character. Not in []. |
| 139 |
\& \eL Lowercase till \eE. Not in []. |
| 140 |
\& \en (Logical) newline character. |
| 141 |
\& \eN Match any character but newline. Not in []. |
| 142 |
\& \eN{} Named or numbered (Unicode) character or sequence. |
| 143 |
\& \eo{} Octal escape sequence. |
| 144 |
\& \ep{}, \epP Match any character with the given Unicode property. |
| 145 |
\& \eP{}, \ePP Match any character without the given property. |
| 146 |
\& \eQ Quote (disable) pattern metacharacters till \eE. Not |
| 147 |
\& in []. |
| 148 |
\& \er Return character. |
| 149 |
\& \eR Generic new line. Not in []. |
| 150 |
\& \es Match any whitespace character. |
| 151 |
\& \eS Match any character that isn\*(Aqt a whitespace. |
| 152 |
\& \et Tab character. |
| 153 |
\& \eu Titlecase next character. Not in []. |
| 154 |
\& \eU Uppercase till \eE. Not in []. |
| 155 |
\& \ev Match any vertical whitespace character. |
| 156 |
\& \eV Match any character that isn\*(Aqt vertical whitespace |
| 157 |
\& \ew Match any word character. |
| 158 |
\& \eW Match any character that isn\*(Aqt a word character. |
| 159 |
\& \ex{}, \ex00 Hexadecimal escape sequence. |
| 160 |
\& \eX Unicode "extended grapheme cluster". Not in []. |
| 161 |
\& \ez End of string. Not in []. |
| 162 |
\& \eZ End of string. Not in []. |
| 163 |
.Ve |
| 164 |
.SS "Character Escapes" |
| 165 |
.IX Subsection "Character Escapes" |
| 166 |
\fIFixed characters\fR |
| 167 |
.IX Subsection "Fixed characters" |
| 168 |
.PP |
| 169 |
A handful of characters have a dedicated \fIcharacter escape\fR. The following |
| 170 |
table shows them, along with their ASCII code points (in decimal and hex), |
| 171 |
their ASCII name, the control escape on ASCII platforms and a short |
| 172 |
description. (For EBCDIC platforms, see "OPERATOR DIFFERENCES" in perlebcdic.) |
| 173 |
.PP |
| 174 |
.Vb 9 |
| 175 |
\& Seq. Code Point ASCII Cntrl Description. |
| 176 |
\& Dec Hex |
| 177 |
\& \ea 7 07 BEL \ecG alarm or bell |
| 178 |
\& \eb 8 08 BS \ecH backspace [1] |
| 179 |
\& \ee 27 1B ESC \ec[ escape character |
| 180 |
\& \ef 12 0C FF \ecL form feed |
| 181 |
\& \en 10 0A LF \ecJ line feed [2] |
| 182 |
\& \er 13 0D CR \ecM carriage return |
| 183 |
\& \et 9 09 TAB \ecI tab |
| 184 |
.Ve |
| 185 |
.IP [1] 4 |
| 186 |
.IX Item "[1]" |
| 187 |
\&\f(CW\*(C`\eb\*(C'\fR is the backspace character only inside a character class. Outside a |
| 188 |
character class, \f(CW\*(C`\eb\*(C'\fR alone is a word\-character/non\-word\-character |
| 189 |
boundary, and \f(CW\*(C`\eb{}\*(C'\fR is some other type of boundary. |
| 190 |
.IP [2] 4 |
| 191 |
.IX Item "[2]" |
| 192 |
\&\f(CW\*(C`\en\*(C'\fR matches a logical newline. Perl converts between \f(CW\*(C`\en\*(C'\fR and your |
| 193 |
OS's native newline character when reading from or writing to text files. |
| 194 |
.PP |
| 195 |
Example |
| 196 |
.IX Subsection "Example" |
| 197 |
.PP |
| 198 |
.Vb 1 |
| 199 |
\& $str =~ /\et/; # Matches if $str contains a (horizontal) tab. |
| 200 |
.Ve |
| 201 |
.PP |
| 202 |
\fIControl characters\fR |
| 203 |
.IX Subsection "Control characters" |
| 204 |
.PP |
| 205 |
\&\f(CW\*(C`\ec\*(C'\fR is used to denote a control character; the character following \f(CW\*(C`\ec\*(C'\fR |
| 206 |
determines the value of the construct. For example the value of \f(CW\*(C`\ecA\*(C'\fR is |
| 207 |
\&\f(CWchr(1)\fR, and the value of \f(CW\*(C`\ecb\*(C'\fR is \f(CWchr(2)\fR, etc. |
| 208 |
The gory details are in "Regexp Quote-Like Operators" in perlop. A complete |
| 209 |
list of what \f(CWchr(1)\fR, etc. means for ASCII and EBCDIC platforms is in |
| 210 |
"OPERATOR DIFFERENCES" in perlebcdic. |
| 211 |
.PP |
| 212 |
Note that \f(CW\*(C`\ec\e\*(C'\fR alone at the end of a regular expression (or doubled-quoted |
| 213 |
string) is not valid. The backslash must be followed by another character. |
| 214 |
That is, \f(CW\*(C`\ec\e\fR\f(CIX\fR\f(CW\*(C'\fR means \f(CW\*(C`chr(28) . \*(Aq\fR\f(CIX\fR\f(CW\*(Aq\*(C'\fR for all characters \fIX\fR. |
| 215 |
.PP |
| 216 |
To write platform-independent code, you must use \f(CW\*(C`\eN{\fR\f(CINAME\fR\f(CW}\*(C'\fR instead, like |
| 217 |
\&\f(CW\*(C`\eN{ESCAPE}\*(C'\fR or \f(CW\*(C`\eN{U+001B}\*(C'\fR, see charnames. |
| 218 |
.PP |
| 219 |
Mnemonic: \fIc\fRontrol character. |
| 220 |
.PP |
| 221 |
Example |
| 222 |
.IX Subsection "Example" |
| 223 |
.PP |
| 224 |
.Vb 1 |
| 225 |
\& $str =~ /\ecK/; # Matches if $str contains a vertical tab (control\-K). |
| 226 |
.Ve |
| 227 |
.PP |
| 228 |
\fINamed or numbered characters and character sequences\fR |
| 229 |
.IX Subsection "Named or numbered characters and character sequences" |
| 230 |
.PP |
| 231 |
Unicode characters have a Unicode name and numeric code point (ordinal) |
| 232 |
value. Use the |
| 233 |
\&\f(CW\*(C`\eN{}\*(C'\fR construct to specify a character by either of these values. |
| 234 |
Certain sequences of characters also have names. |
| 235 |
.PP |
| 236 |
To specify by name, the name of the character or character sequence goes |
| 237 |
between the curly braces. |
| 238 |
.PP |
| 239 |
To specify a character by Unicode code point, use the form \f(CW\*(C`\eN{U+\fR\f(CIcode |
| 240 |
point\fR\f(CW}\*(C'\fR, where \fIcode point\fR is a number in hexadecimal that gives the |
| 241 |
code point that Unicode has assigned to the desired character. It is |
| 242 |
customary but not required to use leading zeros to pad the number to 4 |
| 243 |
digits. Thus \f(CW\*(C`\eN{U+0041}\*(C'\fR means \f(CW\*(C`LATIN CAPITAL LETTER A\*(C'\fR, and you will |
| 244 |
rarely see it written without the two leading zeros. \f(CW\*(C`\eN{U+0041}\*(C'\fR means |
| 245 |
"A" even on EBCDIC machines (where the ordinal value of "A" is not 0x41). |
| 246 |
.PP |
| 247 |
Blanks may freely be inserted adjacent to but within the braces |
| 248 |
enclosing the name or code point. So \f(CW\*(C`\eN{\ U+0041\ }\*(C'\fR is perfectly |
| 249 |
legal. |
| 250 |
.PP |
| 251 |
It is even possible to give your own names to characters and character |
| 252 |
sequences by using the charnames module. These custom names are |
| 253 |
lexically scoped, and so a given code point may have different names |
| 254 |
in different scopes. The name used is what is in effect at the time the |
| 255 |
\&\f(CW\*(C`\eN{}\*(C'\fR is expanded. For patterns in double-quotish context, that means |
| 256 |
at the time the pattern is parsed. But for patterns that are delimitted |
| 257 |
by single quotes, the expansion is deferred until pattern compilation |
| 258 |
time, which may very well have a different \f(CW\*(C`charnames\*(C'\fR translator in |
| 259 |
effect. |
| 260 |
.PP |
| 261 |
(There is an expanded internal form that you may see in debug output: |
| 262 |
\&\f(CW\*(C`\eN{U+\fR\f(CIcode point\fR\f(CW.\fR\f(CIcode point\fR\f(CW...}\*(C'\fR. |
| 263 |
The \f(CW\*(C`...\*(C'\fR means any number of these \fIcode point\fRs separated by dots. |
| 264 |
This represents the sequence formed by the characters. This is an internal |
| 265 |
form only, subject to change, and you should not try to use it yourself.) |
| 266 |
.PP |
| 267 |
Mnemonic: \fIN\fRamed character. |
| 268 |
.PP |
| 269 |
Note that a character or character sequence expressed as a named |
| 270 |
or numbered character is considered a character without special |
| 271 |
meaning by the regex engine, and will match "as is". |
| 272 |
.PP |
| 273 |
Example |
| 274 |
.IX Subsection "Example" |
| 275 |
.PP |
| 276 |
.Vb 1 |
| 277 |
\& $str =~ /\eN{THAI CHARACTER SO SO}/; # Matches the Thai SO SO character |
| 278 |
\& |
| 279 |
\& use charnames \*(AqCyrillic\*(Aq; # Loads Cyrillic names. |
| 280 |
\& $str =~ /\eN{ZHE}\eN{KA}/; # Match "ZHE" followed by "KA". |
| 281 |
.Ve |
| 282 |
.PP |
| 283 |
\fIOctal escapes\fR |
| 284 |
.IX Subsection "Octal escapes" |
| 285 |
.PP |
| 286 |
There are two forms of octal escapes. Each is used to specify a character by |
| 287 |
its code point specified in base 8. |
| 288 |
.PP |
| 289 |
One form, available starting in Perl 5.14 looks like \f(CW\*(C`\eo{...}\*(C'\fR, where the dots |
| 290 |
represent one or more octal digits. It can be used for any Unicode character. |
| 291 |
.PP |
| 292 |
It was introduced to avoid the potential problems with the other form, |
| 293 |
available in all Perls. That form consists of a backslash followed by three |
| 294 |
octal digits. One problem with this form is that it can look exactly like an |
| 295 |
old-style backreference (see |
| 296 |
"Disambiguation rules between old-style octal escapes and backreferences" |
| 297 |
below.) You can avoid this by making the first of the three digits always a |
| 298 |
zero, but that makes \e077 the largest code point specifiable. |
| 299 |
.PP |
| 300 |
In some contexts, a backslash followed by two or even one octal digits may be |
| 301 |
interpreted as an octal escape, sometimes with a warning, and because of some |
| 302 |
bugs, sometimes with surprising results. Also, if you are creating a regex |
| 303 |
out of smaller snippets concatenated together, and you use fewer than three |
| 304 |
digits, the beginning of one snippet may be interpreted as adding digits to the |
| 305 |
ending of the snippet before it. See "Absolute referencing" for more |
| 306 |
discussion and examples of the snippet problem. |
| 307 |
.PP |
| 308 |
Note that a character expressed as an octal escape is considered |
| 309 |
a character without special meaning by the regex engine, and will match |
| 310 |
"as is". |
| 311 |
.PP |
| 312 |
To summarize, the \f(CW\*(C`\eo{}\*(C'\fR form is always safe to use, and the other form is |
| 313 |
safe to use for code points through \e077 when you use exactly three digits to |
| 314 |
specify them. |
| 315 |
.PP |
| 316 |
Mnemonic: \fI0\fRctal or \fIo\fRctal. |
| 317 |
.PP |
| 318 |
Examples (assuming an ASCII platform) |
| 319 |
.IX Subsection "Examples (assuming an ASCII platform)" |
| 320 |
.PP |
| 321 |
.Vb 12 |
| 322 |
\& $str = "Perl"; |
| 323 |
\& $str =~ /\eo{120}/; # Match, "\e120" is "P". |
| 324 |
\& $str =~ /\e120/; # Same. |
| 325 |
\& $str =~ /\eo{120}+/; # Match, "\e120" is "P", |
| 326 |
\& # it\*(Aqs repeated at least once. |
| 327 |
\& $str =~ /\e120+/; # Same. |
| 328 |
\& $str =~ /P\e053/; # No match, "\e053" is "+" and taken literally. |
| 329 |
\& /\eo{23073}/ # Black foreground, white background smiling face. |
| 330 |
\& /\eo{4801234567}/ # Raises a warning, and yields chr(4). |
| 331 |
\& /\eo{ 400}/ # LATIN CAPITAL LETTER A WITH MACRON |
| 332 |
\& /\eo{ 400 }/ # Same. These show blanks are allowed adjacent to |
| 333 |
\& # the braces |
| 334 |
.Ve |
| 335 |
.PP |
| 336 |
Disambiguation rules between old-style octal escapes and backreferences |
| 337 |
.IX Subsection "Disambiguation rules between old-style octal escapes and backreferences" |
| 338 |
.PP |
| 339 |
Octal escapes of the \f(CW\*(C`\e000\*(C'\fR form outside of bracketed character classes |
| 340 |
potentially clash with old-style backreferences (see "Absolute referencing" |
| 341 |
below). They both consist of a backslash followed by numbers. So Perl has to |
| 342 |
use heuristics to determine whether it is a backreference or an octal escape. |
| 343 |
Perl uses the following rules to disambiguate: |
| 344 |
.IP 1. 4 |
| 345 |
If the backslash is followed by a single digit, it's a backreference. |
| 346 |
.IP 2. 4 |
| 347 |
If the first digit following the backslash is a 0, it's an octal escape. |
| 348 |
.IP 3. 4 |
| 349 |
If the number following the backslash is N (in decimal), and Perl already |
| 350 |
has seen N capture groups, Perl considers this a backreference. Otherwise, |
| 351 |
it considers it an octal escape. If N has more than three digits, Perl |
| 352 |
takes only the first three for the octal escape; the rest are matched as is. |
| 353 |
.Sp |
| 354 |
.Vb 6 |
| 355 |
\& my $pat = "(" x 999; |
| 356 |
\& $pat .= "a"; |
| 357 |
\& $pat .= ")" x 999; |
| 358 |
\& /^($pat)\e1000$/; # Matches \*(Aqaa\*(Aq; there are 1000 capture groups. |
| 359 |
\& /^$pat\e1000$/; # Matches \*(Aqa@0\*(Aq; there are 999 capture groups |
| 360 |
\& # and \e1000 is seen as \e100 (a \*(Aq@\*(Aq) and a \*(Aq0\*(Aq. |
| 361 |
.Ve |
| 362 |
.PP |
| 363 |
You can force a backreference interpretation always by using the \f(CW\*(C`\eg{...}\*(C'\fR |
| 364 |
form. You can the force an octal interpretation always by using the \f(CW\*(C`\eo{...}\*(C'\fR |
| 365 |
form, or for numbers up through \e077 (= 63 decimal), by using three digits, |
| 366 |
beginning with a "0". |
| 367 |
.PP |
| 368 |
\fIHexadecimal escapes\fR |
| 369 |
.IX Subsection "Hexadecimal escapes" |
| 370 |
.PP |
| 371 |
Like octal escapes, there are two forms of hexadecimal escapes, but both start |
| 372 |
with the sequence \f(CW\*(C`\ex\*(C'\fR. This is followed by either exactly two hexadecimal |
| 373 |
digits forming a number, or a hexadecimal number of arbitrary length surrounded |
| 374 |
by curly braces. The hexadecimal number is the code point of the character you |
| 375 |
want to express. |
| 376 |
.PP |
| 377 |
Note that a character expressed as one of these escapes is considered a |
| 378 |
character without special meaning by the regex engine, and will match |
| 379 |
"as is". |
| 380 |
.PP |
| 381 |
Mnemonic: he\fIx\fRadecimal. |
| 382 |
.PP |
| 383 |
Examples (assuming an ASCII platform) |
| 384 |
.IX Subsection "Examples (assuming an ASCII platform)" |
| 385 |
.PP |
| 386 |
.Vb 4 |
| 387 |
\& $str = "Perl"; |
| 388 |
\& $str =~ /\ex50/; # Match, "\ex50" is "P". |
| 389 |
\& $str =~ /\ex50+/; # Match, "\ex50" is "P", it is repeated at least once |
| 390 |
\& $str =~ /P\ex2B/; # No match, "\ex2B" is "+" and taken literally. |
| 391 |
\& |
| 392 |
\& /\ex{2603}\ex{2602}/ # Snowman with an umbrella. |
| 393 |
\& # The Unicode character 2603 is a snowman, |
| 394 |
\& # the Unicode character 2602 is an umbrella. |
| 395 |
\& /\ex{263B}/ # Black smiling face. |
| 396 |
\& /\ex{263b}/ # Same, the hex digits A \- F are case insensitive. |
| 397 |
\& /\ex{ 263b }/ # Same, showing optional blanks adjacent to the |
| 398 |
\& # braces |
| 399 |
.Ve |
| 400 |
.SS Modifiers |
| 401 |
.IX Subsection "Modifiers" |
| 402 |
A number of backslash sequences have to do with changing the character, |
| 403 |
or characters following them. \f(CW\*(C`\el\*(C'\fR will lowercase the character following |
| 404 |
it, while \f(CW\*(C`\eu\*(C'\fR will uppercase (or, more accurately, titlecase) the |
| 405 |
character following it. They provide functionality similar to the |
| 406 |
functions \f(CW\*(C`lcfirst\*(C'\fR and \f(CW\*(C`ucfirst\*(C'\fR. |
| 407 |
.PP |
| 408 |
To uppercase or lowercase several characters, one might want to use |
| 409 |
\&\f(CW\*(C`\eL\*(C'\fR or \f(CW\*(C`\eU\*(C'\fR, which will lowercase/uppercase all characters following |
| 410 |
them, until either the end of the pattern or the next occurrence of |
| 411 |
\&\f(CW\*(C`\eE\*(C'\fR, whichever comes first. They provide functionality similar to what |
| 412 |
the functions \f(CW\*(C`lc\*(C'\fR and \f(CW\*(C`uc\*(C'\fR provide. |
| 413 |
.PP |
| 414 |
\&\f(CW\*(C`\eQ\*(C'\fR is used to quote (disable) pattern metacharacters, up to the next |
| 415 |
\&\f(CW\*(C`\eE\*(C'\fR or the end of the pattern. \f(CW\*(C`\eQ\*(C'\fR adds a backslash to any character |
| 416 |
that could have special meaning to Perl. In the ASCII range, it quotes |
| 417 |
every character that isn't a letter, digit, or underscore. See |
| 418 |
"quotemeta" in perlfunc for details on what gets quoted for non-ASCII |
| 419 |
code points. Using this ensures that any character between \f(CW\*(C`\eQ\*(C'\fR and |
| 420 |
\&\f(CW\*(C`\eE\*(C'\fR will be matched literally, not interpreted as a metacharacter by |
| 421 |
the regex engine. |
| 422 |
.PP |
| 423 |
\&\f(CW\*(C`\eF\*(C'\fR can be used to casefold all characters following, up to the next \f(CW\*(C`\eE\*(C'\fR |
| 424 |
or the end of the pattern. It provides the functionality similar to |
| 425 |
the \f(CW\*(C`fc\*(C'\fR function. |
| 426 |
.PP |
| 427 |
Mnemonic: \fIL\fRowercase, \fIU\fRppercase, \fIF\fRold-case, \fIQ\fRuotemeta, \fIE\fRnd. |
| 428 |
.PP |
| 429 |
Examples |
| 430 |
.IX Subsection "Examples" |
| 431 |
.PP |
| 432 |
.Vb 7 |
| 433 |
\& $sid = "sid"; |
| 434 |
\& $greg = "GrEg"; |
| 435 |
\& $miranda = "(Miranda)"; |
| 436 |
\& $str =~ /\eu$sid/; # Matches \*(AqSid\*(Aq |
| 437 |
\& $str =~ /\eL$greg/; # Matches \*(Aqgreg\*(Aq |
| 438 |
\& $str =~ /\eQ$miranda\eE/; # Matches \*(Aq(Miranda)\*(Aq, as if the pattern |
| 439 |
\& # had been written as /\e(Miranda\e)/ |
| 440 |
.Ve |
| 441 |
.SS "Character classes" |
| 442 |
.IX Subsection "Character classes" |
| 443 |
Perl regular expressions have a large range of character classes. Some of |
| 444 |
the character classes are written as a backslash sequence. We will briefly |
| 445 |
discuss those here; full details of character classes can be found in |
| 446 |
perlrecharclass. |
| 447 |
.PP |
| 448 |
\&\f(CW\*(C`\ew\*(C'\fR is a character class that matches any single \fIword\fR character |
| 449 |
(letters, digits, Unicode marks, and connector punctuation (like the |
| 450 |
underscore)). \f(CW\*(C`\ed\*(C'\fR is a character class that matches any decimal |
| 451 |
digit, while the character class \f(CW\*(C`\es\*(C'\fR matches any whitespace character. |
| 452 |
New in perl 5.10.0 are the classes \f(CW\*(C`\eh\*(C'\fR and \f(CW\*(C`\ev\*(C'\fR which match horizontal |
| 453 |
and vertical whitespace characters. |
| 454 |
.PP |
| 455 |
The exact set of characters matched by \f(CW\*(C`\ed\*(C'\fR, \f(CW\*(C`\es\*(C'\fR, and \f(CW\*(C`\ew\*(C'\fR varies |
| 456 |
depending on various pragma and regular expression modifiers. It is |
| 457 |
possible to restrict the match to the ASCII range by using the \f(CW\*(C`/a\*(C'\fR |
| 458 |
regular expression modifier. See perlrecharclass. |
| 459 |
.PP |
| 460 |
The uppercase variants (\f(CW\*(C`\eW\*(C'\fR, \f(CW\*(C`\eD\*(C'\fR, \f(CW\*(C`\eS\*(C'\fR, \f(CW\*(C`\eH\*(C'\fR, and \f(CW\*(C`\eV\*(C'\fR) are |
| 461 |
character classes that match, respectively, any character that isn't a |
| 462 |
word character, digit, whitespace, horizontal whitespace, or vertical |
| 463 |
whitespace. |
| 464 |
.PP |
| 465 |
Mnemonics: \fIw\fRord, \fId\fRigit, \fIs\fRpace, \fIh\fRorizontal, \fIv\fRertical. |
| 466 |
.PP |
| 467 |
\fIUnicode classes\fR |
| 468 |
.IX Subsection "Unicode classes" |
| 469 |
.PP |
| 470 |
\&\f(CW\*(C`\epP\*(C'\fR (where \f(CW\*(C`P\*(C'\fR is a single letter) and \f(CW\*(C`\ep{Property}\*(C'\fR are used to |
| 471 |
match a character that matches the given Unicode property; properties |
| 472 |
include things like "letter", or "thai character". Capitalizing the |
| 473 |
sequence to \f(CW\*(C`\ePP\*(C'\fR and \f(CW\*(C`\eP{Property}\*(C'\fR make the sequence match a character |
| 474 |
that doesn't match the given Unicode property. For more details, see |
| 475 |
"Backslash sequences" in perlrecharclass and |
| 476 |
"Unicode Character Properties" in perlunicode. |
| 477 |
.PP |
| 478 |
Mnemonic: \fIp\fRroperty. |
| 479 |
.SS Referencing |
| 480 |
.IX Subsection "Referencing" |
| 481 |
If capturing parenthesis are used in a regular expression, we can refer |
| 482 |
to the part of the source string that was matched, and match exactly the |
| 483 |
same thing. There are three ways of referring to such \fIbackreference\fR: |
| 484 |
absolutely, relatively, and by name. |
| 485 |
.PP |
| 486 |
\fIAbsolute referencing\fR |
| 487 |
.IX Subsection "Absolute referencing" |
| 488 |
.PP |
| 489 |
Either \f(CW\*(C`\eg\fR\f(CIN\fR\f(CW\*(C'\fR (starting in Perl 5.10.0), or \f(CW\*(C`\e\fR\f(CIN\fR\f(CW\*(C'\fR (old-style) where \fIN\fR |
| 490 |
is a positive (unsigned) decimal number of any length is an absolute reference |
| 491 |
to a capturing group. |
| 492 |
.PP |
| 493 |
\&\fIN\fR refers to the Nth set of parentheses, so \f(CW\*(C`\eg\fR\f(CIN\fR\f(CW\*(C'\fR refers to whatever has |
| 494 |
been matched by that set of parentheses. Thus \f(CW\*(C`\eg1\*(C'\fR refers to the first |
| 495 |
capture group in the regex. |
| 496 |
.PP |
| 497 |
The \f(CW\*(C`\eg\fR\f(CIN\fR\f(CW\*(C'\fR form can be equivalently written as \f(CW\*(C`\eg{\fR\f(CIN\fR\f(CW}\*(C'\fR |
| 498 |
which avoids ambiguity when building a regex by concatenating shorter |
| 499 |
strings. Otherwise if you had a regex \f(CW\*(C`qr/$x$y/\*(C'\fR, and \f(CW$x\fR contained |
| 500 |
\&\f(CW"\eg1"\fR, and \f(CW$y\fR contained \f(CW"37"\fR, you would get \f(CW\*(C`/\eg137/\*(C'\fR which is |
| 501 |
probably not what you intended. |
| 502 |
.PP |
| 503 |
In the \f(CW\*(C`\e\fR\f(CIN\fR\f(CW\*(C'\fR form, \fIN\fR must not begin with a "0", and there must be at |
| 504 |
least \fIN\fR capturing groups, or else \fIN\fR is considered an octal escape |
| 505 |
(but something like \f(CW\*(C`\e18\*(C'\fR is the same as \f(CW\*(C`\e0018\*(C'\fR; that is, the octal escape |
| 506 |
\&\f(CW"\e001"\fR followed by a literal digit \f(CW"8"\fR). |
| 507 |
.PP |
| 508 |
Mnemonic: \fIg\fRroup. |
| 509 |
.PP |
| 510 |
Examples |
| 511 |
.IX Subsection "Examples" |
| 512 |
.PP |
| 513 |
.Vb 5 |
| 514 |
\& /(\ew+) \eg1/; # Finds a duplicated word, (e.g. "cat cat"). |
| 515 |
\& /(\ew+) \e1/; # Same thing; written old\-style. |
| 516 |
\& /(\ew+) \eg{1}/; # Same, using the safer braced notation |
| 517 |
\& /(\ew+) \eg{ 1 }/;# Same, showing optional blanks adjacent to the braces |
| 518 |
\& /(.)(.)\eg2\eg1/; # Match a four letter palindrome (e.g. "ABBA"). |
| 519 |
.Ve |
| 520 |
.PP |
| 521 |
\fIRelative referencing\fR |
| 522 |
.IX Subsection "Relative referencing" |
| 523 |
.PP |
| 524 |
\&\f(CW\*(C`\eg\-\fR\f(CIN\fR\f(CW\*(C'\fR (starting in Perl 5.10.0) is used for relative addressing. (It can |
| 525 |
be written as \f(CW\*(C`\eg{\-\fR\f(CIN\fR\f(CW}\*(C'\fR.) It refers to the \fIN\fRth group before the |
| 526 |
\&\f(CW\*(C`\eg{\-\fR\f(CIN\fR\f(CW}\*(C'\fR. |
| 527 |
.PP |
| 528 |
The big advantage of this form is that it makes it much easier to write |
| 529 |
patterns with references that can be interpolated in larger patterns, |
| 530 |
even if the larger pattern also contains capture groups. |
| 531 |
.PP |
| 532 |
Examples |
| 533 |
.IX Subsection "Examples" |
| 534 |
.PP |
| 535 |
.Vb 8 |
| 536 |
\& /(A) # Group 1 |
| 537 |
\& ( # Group 2 |
| 538 |
\& (B) # Group 3 |
| 539 |
\& \eg{\-1} # Refers to group 3 (B) |
| 540 |
\& \eg{\-3} # Refers to group 1 (A) |
| 541 |
\& \eg{ \-3 } # Same, showing optional blanks adjacent to the braces |
| 542 |
\& ) |
| 543 |
\& /x; # Matches "ABBA". |
| 544 |
\& |
| 545 |
\& my $qr = qr /(.)(.)\eg{\-2}\eg{\-1}/; # Matches \*(Aqabab\*(Aq, \*(Aqcdcd\*(Aq, etc. |
| 546 |
\& /$qr$qr/ # Matches \*(Aqababcdcd\*(Aq. |
| 547 |
.Ve |
| 548 |
.PP |
| 549 |
\fINamed referencing\fR |
| 550 |
.IX Subsection "Named referencing" |
| 551 |
.PP |
| 552 |
\&\f(CW\*(C`\eg{\fR\f(CIname\fR\f(CW}\*(C'\fR (starting in Perl 5.10.0) can be used to back refer to a |
| 553 |
named capture group, dispensing completely with having to think about capture |
| 554 |
buffer positions. |
| 555 |
.PP |
| 556 |
To be compatible with .Net regular expressions, \f(CW\*(C`\eg{name}\*(C'\fR may also be |
| 557 |
written as \f(CW\*(C`\ek{name}\*(C'\fR, \f(CW\*(C`\ek<name>\*(C'\fR or \f(CW\*(C`\ek\*(Aqname\*(Aq\*(C'\fR. |
| 558 |
.PP |
| 559 |
To prevent any ambiguity, \fIname\fR must not start with a digit nor contain a |
| 560 |
hyphen. |
| 561 |
.PP |
| 562 |
Examples |
| 563 |
.IX Subsection "Examples" |
| 564 |
.PP |
| 565 |
.Vb 10 |
| 566 |
\& /(?<word>\ew+) \eg{word}/ # Finds duplicated word, (e.g. "cat cat") |
| 567 |
\& /(?<word>\ew+) \ek{word}/ # Same. |
| 568 |
\& /(?<word>\ew+) \eg{ word }/ # Same, showing optional blanks adjacent to |
| 569 |
\& # the braces |
| 570 |
\& /(?<word>\ew+) \ek{ word }/ # Same. |
| 571 |
\& /(?<word>\ew+) \ek<word>/ # Same. There are no braces, so no blanks |
| 572 |
\& # are permitted |
| 573 |
\& /(?<letter1>.)(?<letter2>.)\eg{letter2}\eg{letter1}/ |
| 574 |
\& # Match a four letter palindrome (e.g. |
| 575 |
\& # "ABBA") |
| 576 |
.Ve |
| 577 |
.SS Assertions |
| 578 |
.IX Subsection "Assertions" |
| 579 |
Assertions are conditions that have to be true; they don't actually |
| 580 |
match parts of the substring. There are six assertions that are written as |
| 581 |
backslash sequences. |
| 582 |
.IP \eA 4 |
| 583 |
.IX Item "A" |
| 584 |
\&\f(CW\*(C`\eA\*(C'\fR only matches at the beginning of the string. If the \f(CW\*(C`/m\*(C'\fR modifier |
| 585 |
isn't used, then \f(CW\*(C`/\eA/\*(C'\fR is equivalent to \f(CW\*(C`/^/\*(C'\fR. However, if the \f(CW\*(C`/m\*(C'\fR |
| 586 |
modifier is used, then \f(CW\*(C`/^/\*(C'\fR matches internal newlines, but the meaning |
| 587 |
of \f(CW\*(C`/\eA/\*(C'\fR isn't changed by the \f(CW\*(C`/m\*(C'\fR modifier. \f(CW\*(C`\eA\*(C'\fR matches at the beginning |
| 588 |
of the string regardless whether the \f(CW\*(C`/m\*(C'\fR modifier is used. |
| 589 |
.IP "\ez, \eZ" 4 |
| 590 |
.IX Item "z, Z" |
| 591 |
\&\f(CW\*(C`\ez\*(C'\fR and \f(CW\*(C`\eZ\*(C'\fR match at the end of the string. If the \f(CW\*(C`/m\*(C'\fR modifier isn't |
| 592 |
used, then \f(CW\*(C`/\eZ/\*(C'\fR is equivalent to \f(CW\*(C`/$/\*(C'\fR; that is, it matches at the |
| 593 |
end of the string, or one before the newline at the end of the string. If the |
| 594 |
\&\f(CW\*(C`/m\*(C'\fR modifier is used, then \f(CW\*(C`/$/\*(C'\fR matches at internal newlines, but the |
| 595 |
meaning of \f(CW\*(C`/\eZ/\*(C'\fR isn't changed by the \f(CW\*(C`/m\*(C'\fR modifier. \f(CW\*(C`\eZ\*(C'\fR matches at |
| 596 |
the end of the string (or just before a trailing newline) regardless whether |
| 597 |
the \f(CW\*(C`/m\*(C'\fR modifier is used. |
| 598 |
.Sp |
| 599 |
\&\f(CW\*(C`\ez\*(C'\fR is just like \f(CW\*(C`\eZ\*(C'\fR, except that it does not match before a trailing |
| 600 |
newline. \f(CW\*(C`\ez\*(C'\fR matches at the end of the string only, regardless of the |
| 601 |
modifiers used, and not just before a newline. It is how to anchor the |
| 602 |
match to the true end of the string under all conditions. |
| 603 |
.IP \eG 4 |
| 604 |
.IX Item "G" |
| 605 |
\&\f(CW\*(C`\eG\*(C'\fR is usually used only in combination with the \f(CW\*(C`/g\*(C'\fR modifier. If the |
| 606 |
\&\f(CW\*(C`/g\*(C'\fR modifier is used and the match is done in scalar context, Perl |
| 607 |
remembers where in the source string the last match ended, and the next time, |
| 608 |
it will start the match from where it ended the previous time. |
| 609 |
.Sp |
| 610 |
\&\f(CW\*(C`\eG\*(C'\fR matches the point where the previous match on that string ended, |
| 611 |
or the beginning of that string if there was no previous match. |
| 612 |
.Sp |
| 613 |
Mnemonic: \fIG\fRlobal. |
| 614 |
.IP "\eb{}, \eb, \eB{}, \eB" 4 |
| 615 |
.IX Item "b{}, b, B{}, B" |
| 616 |
\&\f(CW\*(C`\eb{...}\*(C'\fR, available starting in v5.22, matches a boundary (between two |
| 617 |
characters, or before the first character of the string, or after the |
| 618 |
final character of the string) based on the Unicode rules for the |
| 619 |
boundary type specified inside the braces. The boundary |
| 620 |
types are given a few paragraphs below. \f(CW\*(C`\eB{...}\*(C'\fR matches at any place |
| 621 |
between characters where \f(CW\*(C`\eb{...}\*(C'\fR of the same type doesn't match. |
| 622 |
.Sp |
| 623 |
\&\f(CW\*(C`\eb\*(C'\fR when not immediately followed by a \f(CW"{"\fR is available in all |
| 624 |
Perls. It matches at any place |
| 625 |
between a word (something matched by \f(CW\*(C`\ew\*(C'\fR) and a non-word character |
| 626 |
(\f(CW\*(C`\eW\*(C'\fR); \f(CW\*(C`\eB\*(C'\fR when not immediately followed by a \f(CW"{"\fR matches at any |
| 627 |
place between characters where \f(CW\*(C`\eb\*(C'\fR doesn't match. To get better |
| 628 |
word matching of natural language text, see "\eb{wb}" below. |
| 629 |
.Sp |
| 630 |
\&\f(CW\*(C`\eb\*(C'\fR |
| 631 |
and \f(CW\*(C`\eB\*(C'\fR assume there's a non-word character before the beginning and after |
| 632 |
the end of the source string; so \f(CW\*(C`\eb\*(C'\fR will match at the beginning (or end) |
| 633 |
of the source string if the source string begins (or ends) with a word |
| 634 |
character. Otherwise, \f(CW\*(C`\eB\*(C'\fR will match. |
| 635 |
.Sp |
| 636 |
Do not use something like \f(CW\*(C`\eb=head\ed\eb\*(C'\fR and expect it to match the |
| 637 |
beginning of a line. It can't, because for there to be a boundary before |
| 638 |
the non-word "=", there must be a word character immediately previous. |
| 639 |
All plain \f(CW\*(C`\eb\*(C'\fR and \f(CW\*(C`\eB\*(C'\fR boundary determinations look for word |
| 640 |
characters alone, not for |
| 641 |
non-word characters nor for string ends. It may help to understand how |
| 642 |
\&\f(CW\*(C`\eb\*(C'\fR and \f(CW\*(C`\eB\*(C'\fR work by equating them as follows: |
| 643 |
.Sp |
| 644 |
.Vb 2 |
| 645 |
\& \eb really means (?:(?<=\ew)(?!\ew)|(?<!\ew)(?=\ew)) |
| 646 |
\& \eB really means (?:(?<=\ew)(?=\ew)|(?<!\ew)(?!\ew)) |
| 647 |
.Ve |
| 648 |
.Sp |
| 649 |
In contrast, \f(CW\*(C`\eb{...}\*(C'\fR and \f(CW\*(C`\eB{...}\*(C'\fR may or may not match at the |
| 650 |
beginning and end of the line, depending on the boundary type. These |
| 651 |
implement the Unicode default boundaries, specified in |
| 652 |
<https://www.unicode.org/reports/tr14/> and |
| 653 |
<https://www.unicode.org/reports/tr29/>. |
| 654 |
The boundary types are: |
| 655 |
.RS 4 |
| 656 |
.ie n .IP """\eb{gcb}"" or ""\eb{g}""" 4 |
| 657 |
.el .IP "\f(CW\eb{gcb}\fR or \f(CW\eb{g}\fR" 4 |
| 658 |
.IX Item "b{gcb} or b{g}" |
| 659 |
This matches a Unicode "Grapheme Cluster Boundary". (Actually Perl |
| 660 |
always uses the improved "extended" grapheme cluster"). These are |
| 661 |
explained below under \f(CW"\eX"\fR. In fact, \f(CW\*(C`\eX\*(C'\fR is another way to get |
| 662 |
the same functionality. It is equivalent to \f(CW\*(C`/.+?\eb{gcb}/\*(C'\fR. Use |
| 663 |
whichever is most convenient for your situation. |
| 664 |
.ie n .IP """\eb{lb}""" 4 |
| 665 |
.el .IP \f(CW\eb{lb}\fR 4 |
| 666 |
.IX Item "b{lb}" |
| 667 |
This matches according to the default Unicode Line Breaking Algorithm |
| 668 |
(<https://www.unicode.org/reports/tr14/>), as customized in that |
| 669 |
document |
| 670 |
(Example 7 of revision 35 <https://www.unicode.org/reports/tr14/tr14-35.html#Example7>) |
| 671 |
for better handling of numeric expressions. |
| 672 |
.Sp |
| 673 |
This is suitable for many purposes, but the Unicode::LineBreak module |
| 674 |
is available on CPAN that provides many more features, including |
| 675 |
customization. |
| 676 |
.ie n .IP """\eb{sb}""" 4 |
| 677 |
.el .IP \f(CW\eb{sb}\fR 4 |
| 678 |
.IX Item "b{sb}" |
| 679 |
This matches a Unicode "Sentence Boundary". This is an aid to parsing |
| 680 |
natural language sentences. It gives good, but imperfect results. For |
| 681 |
example, it thinks that "Mr. Smith" is two sentences. More details are |
| 682 |
at <https://www.unicode.org/reports/tr29/>. Note also that it thinks |
| 683 |
that anything matching "\eR" (except form feed and vertical tab) is a |
| 684 |
sentence boundary. \f(CW\*(C`\eb{sb}\*(C'\fR works with text designed for |
| 685 |
word-processors which wrap lines |
| 686 |
automatically for display, but hard-coded line boundaries are considered |
| 687 |
to be essentially the ends of text blocks (paragraphs really), and hence |
| 688 |
the ends of sentences. \f(CW\*(C`\eb{sb}\*(C'\fR doesn't do well with text containing |
| 689 |
embedded newlines, like the source text of the document you are reading. |
| 690 |
Such text needs to be preprocessed to get rid of the line separators |
| 691 |
before looking for sentence boundaries. Some people view this as a bug |
| 692 |
in the Unicode standard, and this behavior is quite subject to change in |
| 693 |
future Perl versions. |
| 694 |
.ie n .IP """\eb{wb}""" 4 |
| 695 |
.el .IP \f(CW\eb{wb}\fR 4 |
| 696 |
.IX Item "b{wb}" |
| 697 |
This matches a Unicode "Word Boundary", but tailored to Perl |
| 698 |
expectations. This gives better (though not |
| 699 |
perfect) results for natural language processing than plain \f(CW\*(C`\eb\*(C'\fR |
| 700 |
(without braces) does. For example, it understands that apostrophes can |
| 701 |
be in the middle of words and that parentheses aren't (see the examples |
| 702 |
below). More details are at <https://www.unicode.org/reports/tr29/>. |
| 703 |
.Sp |
| 704 |
The current Unicode definition of a Word Boundary matches between every |
| 705 |
white space character. Perl tailors this, starting in version 5.24, to |
| 706 |
generally not break up spans of white space, just as plain \f(CW\*(C`\eb\*(C'\fR has |
| 707 |
always functioned. This allows \f(CW\*(C`\eb{wb}\*(C'\fR to be a drop-in replacement for |
| 708 |
\&\f(CW\*(C`\eb\*(C'\fR, but with generally better results for natural language |
| 709 |
processing. (The exception to this tailoring is when a span of white |
| 710 |
space is immediately followed by something like U+0303, COMBINING TILDE. |
| 711 |
If the final space character in the span is a horizontal white space, it |
| 712 |
is broken out so that it attaches instead to the combining character. |
| 713 |
To be precise, if a span of white space that ends in a horizontal space |
| 714 |
has the character immediately following it have any of the Word |
| 715 |
Boundary property values "Extend", "Format" or "ZWJ", the boundary between the |
| 716 |
final horizontal space character and the rest of the span matches |
| 717 |
\&\f(CW\*(C`\eb{wb}\*(C'\fR. In all other cases the boundary between two white space |
| 718 |
characters matches \f(CW\*(C`\eB{wb}\*(C'\fR.) |
| 719 |
.RE |
| 720 |
.RS 4 |
| 721 |
.Sp |
| 722 |
It is important to realize when you use these Unicode boundaries, |
| 723 |
that you are taking a risk that a future version of Perl which contains |
| 724 |
a later version of the Unicode Standard will not work precisely the same |
| 725 |
way as it did when your code was written. These rules are not |
| 726 |
considered stable and have been somewhat more subject to change than the |
| 727 |
rest of the Standard. Unicode reserves the right to change them at |
| 728 |
will, and Perl reserves the right to update its implementation to |
| 729 |
Unicode's new rules. In the past, some changes have been because new |
| 730 |
characters have been added to the Standard which have different |
| 731 |
characteristics than all previous characters, so new rules are |
| 732 |
formulated for handling them. These should not cause any backward |
| 733 |
compatibility issues. But some changes have changed the treatment of |
| 734 |
existing characters because the Unicode Technical Committee has decided |
| 735 |
that the change is warranted for whatever reason. This could be to fix |
| 736 |
a bug, or because they think better results are obtained with the new |
| 737 |
rule. |
| 738 |
.Sp |
| 739 |
It is also important to realize that these are default boundary |
| 740 |
definitions, and that implementations may wish to tailor the results for |
| 741 |
particular purposes and locales. For example, some languages, such as |
| 742 |
Japanese and Thai, require dictionary lookup to accurately determine |
| 743 |
word boundaries. |
| 744 |
.Sp |
| 745 |
Mnemonic: \fIb\fRoundary. |
| 746 |
.RE |
| 747 |
.PP |
| 748 |
Examples |
| 749 |
.IX Subsection "Examples" |
| 750 |
.PP |
| 751 |
.Vb 4 |
| 752 |
\& "cat" =~ /\eAcat/; # Match. |
| 753 |
\& "cat" =~ /cat\eZ/; # Match. |
| 754 |
\& "cat\en" =~ /cat\eZ/; # Match. |
| 755 |
\& "cat\en" =~ /cat\ez/; # No match. |
| 756 |
\& |
| 757 |
\& "cat" =~ /\ebcat\eb/; # Matches. |
| 758 |
\& "cats" =~ /\ebcat\eb/; # No match. |
| 759 |
\& "cat" =~ /\ebcat\eB/; # No match. |
| 760 |
\& "cats" =~ /\ebcat\eB/; # Match. |
| 761 |
\& |
| 762 |
\& while ("cat dog" =~ /(\ew+)/g) { |
| 763 |
\& print $1; # Prints \*(Aqcatdog\*(Aq |
| 764 |
\& } |
| 765 |
\& while ("cat dog" =~ /\eG(\ew+)/g) { |
| 766 |
\& print $1; # Prints \*(Aqcat\*(Aq |
| 767 |
\& } |
| 768 |
\& |
| 769 |
\& my $s = "He said, \e"Is pi 3.14? (I\*(Aqm not sure).\e""; |
| 770 |
\& print join("|", $s =~ m/ ( .+? \eb ) /xg), "\en"; |
| 771 |
\& print join("|", $s =~ m/ ( .+? \eb{wb} ) /xg), "\en"; |
| 772 |
\& prints |
| 773 |
\& He| |said|, "|Is| |pi| |3|.|14|? (|I|\*(Aq|m| |not| |sure |
| 774 |
\& He| |said|,| |"|Is| |pi| |3.14|?| |(|I\*(Aqm| |not| |sure|)|.|" |
| 775 |
.Ve |
| 776 |
.SS Misc |
| 777 |
.IX Subsection "Misc" |
| 778 |
Here we document the backslash sequences that don't fall in one of the |
| 779 |
categories above. These are: |
| 780 |
.IP \eK 4 |
| 781 |
.IX Item "K" |
| 782 |
This appeared in perl 5.10.0. Anything matched left of \f(CW\*(C`\eK\*(C'\fR is |
| 783 |
not included in \f(CW$&\fR, and will not be replaced if the pattern is |
| 784 |
used in a substitution. This lets you write \f(CW\*(C`s/PAT1 \eK PAT2/REPL/x\*(C'\fR |
| 785 |
instead of \f(CW\*(C`s/(PAT1) PAT2/${1}REPL/x\*(C'\fR or \f(CW\*(C`s/(?<=PAT1) PAT2/REPL/x\*(C'\fR. |
| 786 |
.Sp |
| 787 |
Mnemonic: \fIK\fReep. |
| 788 |
.IP \eN 4 |
| 789 |
.IX Item "N" |
| 790 |
This feature, available starting in v5.12, matches any character |
| 791 |
that is \fBnot\fR a newline. It is a short-hand for writing \f(CW\*(C`[^\en]\*(C'\fR, and is |
| 792 |
identical to the \f(CW\*(C`.\*(C'\fR metasymbol, except under the \f(CW\*(C`/s\*(C'\fR flag, which changes |
| 793 |
the meaning of \f(CW\*(C`.\*(C'\fR, but not \f(CW\*(C`\eN\*(C'\fR. |
| 794 |
.Sp |
| 795 |
Note that \f(CW\*(C`\eN{...}\*(C'\fR can mean a |
| 796 |
named or numbered character |
| 797 |
\&. |
| 798 |
.Sp |
| 799 |
Mnemonic: Complement of \fI\en\fR. |
| 800 |
.IP \eR 4 |
| 801 |
.IX Xref "\\R" |
| 802 |
.IX Item "R" |
| 803 |
\&\f(CW\*(C`\eR\*(C'\fR matches a \fIgeneric newline\fR; that is, anything considered a |
| 804 |
linebreak sequence by Unicode. This includes all characters matched by |
| 805 |
\&\f(CW\*(C`\ev\*(C'\fR (vertical whitespace), and the multi character sequence \f(CW"\ex0D\ex0A"\fR |
| 806 |
(carriage return followed by a line feed, sometimes called the network |
| 807 |
newline; it's the end of line sequence used in Microsoft text files opened |
| 808 |
in binary mode). \f(CW\*(C`\eR\*(C'\fR is equivalent to \f(CW\*(C`(?>\ex0D\ex0A|\ev)\*(C'\fR. (The |
| 809 |
reason it doesn't backtrack is that the sequence is considered |
| 810 |
inseparable. That means that |
| 811 |
.Sp |
| 812 |
.Vb 1 |
| 813 |
\& "\ex0D\ex0A" =~ /^\eR\ex0A$/ # No match |
| 814 |
.Ve |
| 815 |
.Sp |
| 816 |
fails, because the \f(CW\*(C`\eR\*(C'\fR matches the entire string, and won't backtrack |
| 817 |
to match just the \f(CW"\ex0D"\fR.) Since |
| 818 |
\&\f(CW\*(C`\eR\*(C'\fR can match a sequence of more than one character, it cannot be put |
| 819 |
inside a bracketed character class; \f(CW\*(C`/[\eR]/\*(C'\fR is an error; use \f(CW\*(C`\ev\*(C'\fR |
| 820 |
instead. \f(CW\*(C`\eR\*(C'\fR was introduced in perl 5.10.0. |
| 821 |
.Sp |
| 822 |
Note that this does not respect any locale that might be in effect; it |
| 823 |
matches according to the platform's native character set. |
| 824 |
.Sp |
| 825 |
Mnemonic: none really. \f(CW\*(C`\eR\*(C'\fR was picked because PCRE already uses \f(CW\*(C`\eR\*(C'\fR, |
| 826 |
and more importantly because Unicode recommends such a regular expression |
| 827 |
metacharacter, and suggests \f(CW\*(C`\eR\*(C'\fR as its notation. |
| 828 |
.IP \eX 4 |
| 829 |
.IX Xref "\\X" |
| 830 |
.IX Item "X" |
| 831 |
This matches a Unicode \fIextended grapheme cluster\fR. |
| 832 |
.Sp |
| 833 |
\&\f(CW\*(C`\eX\*(C'\fR matches quite well what normal (non-Unicode-programmer) usage |
| 834 |
would consider a single character. As an example, consider a G with some sort |
| 835 |
of diacritic mark, such as an arrow. There is no such single character in |
| 836 |
Unicode, but one can be composed by using a G followed by a Unicode "COMBINING |
| 837 |
UPWARDS ARROW BELOW", and would be displayed by Unicode-aware software as if it |
| 838 |
were a single character. |
| 839 |
.Sp |
| 840 |
The match is greedy and non-backtracking, so that the cluster is never |
| 841 |
broken up into smaller components. |
| 842 |
.Sp |
| 843 |
See also \f(CW\*(C`\eb{gcb}\*(C'\fR. |
| 844 |
.Sp |
| 845 |
Mnemonic: e\fIX\fRtended Unicode character. |
| 846 |
.PP |
| 847 |
Examples |
| 848 |
.IX Subsection "Examples" |
| 849 |
.PP |
| 850 |
.Vb 2 |
| 851 |
\& $str =~ s/foo\eKbar/baz/g; # Change any \*(Aqbar\*(Aq following a \*(Aqfoo\*(Aq to \*(Aqbaz\*(Aq |
| 852 |
\& $str =~ s/(.)\eK\eg1//g; # Delete duplicated characters. |
| 853 |
\& |
| 854 |
\& "\en" =~ /^\eR$/; # Match, \en is a generic newline. |
| 855 |
\& "\er" =~ /^\eR$/; # Match, \er is a generic newline. |
| 856 |
\& "\er\en" =~ /^\eR$/; # Match, \er\en is a generic newline. |
| 857 |
\& |
| 858 |
\& "P\ex{307}" =~ /^\eX$/ # \eX matches a P with a dot above. |
| 859 |
.Ve |