Debian Codegraph @

upstream/fedora-44/man1/perlrebackslash.1

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