1PERLREBACKSLASH(1)     Perl Programmers Reference Guide     PERLREBACKSLASH(1)
2
3
4

NAME

6       perlrebackslash - Perl Regular Expression Backslash Sequences and
7       Escapes
8

DESCRIPTION

10       The top level documentation about Perl regular expressions is found in
11       perlre.
12
13       This document describes all backslash and escape sequences. After
14       explaining the role of the backslash, it lists all the sequences that
15       have a special meaning in Perl regular expressions (in alphabetical
16       order), then describes each of them.
17
18       Most sequences are described in detail in different documents; the
19       primary purpose of this document is to have a quick reference guide
20       describing all backslash and escape sequences.
21
22   The backslash
23       In a regular expression, the backslash can perform one of two tasks: it
24       either takes away the special meaning of the character following it
25       (for instance, "\|" matches a vertical bar, it's not an alternation),
26       or it is the start of a backslash or escape sequence.
27
28       The rules determining what it is are quite simple: if the character
29       following the backslash is an ASCII punctuation (non-word) character
30       (that is, anything that is not a letter, digit or underscore), then the
31       backslash just takes away the special meaning (if any) of the character
32       following it.
33
34       If the character following the backslash is an ASCII letter or an ASCII
35       digit, then the sequence may be special; if so, it's listed below. A
36       few letters have not been used yet, so escaping them with a backslash
37       doesn't change them to be special.  A future version of Perl may assign
38       a special meaning to them, so if you have warnings turned on, Perl will
39       issue a warning if you use such a sequence.  [1].
40
41       It is however guaranteed that backslash or escape sequences never have
42       a punctuation character following the backslash, not now, and not in a
43       future version of Perl 5. So it is safe to put a backslash in front of
44       a non-word character.
45
46       Note that the backslash itself is special; if you want to match a
47       backslash, you have to escape the backslash with a backslash: "/\\/"
48       matches a single backslash.
49
50       [1] There is one exception. If you use an alphanumerical character as
51           the delimiter of your pattern (which you probably shouldn't do for
52           readability reasons), you will have to escape the delimiter if you
53           want to match it. Perl won't warn then. See also "Gory details of
54           parsing quoted constructs" in perlop.
55
56   All the sequences and escapes
57       Those not usable within a bracketed character class (like "[\da-z]")
58       are marked as "Not in []."
59
60        \000              Octal escape sequence.
61        \1                Absolute backreference.  Not in [].
62        \a                Alarm or bell.
63        \A                Beginning of string.  Not in [].
64        \b                Word/non-word boundary. (Backspace in []).
65        \B                Not a word/non-word boundary.  Not in [].
66        \cX               Control-X
67        \C                Single octet, even under UTF-8.  Not in [].
68        \d                Character class for digits.
69        \D                Character class for non-digits.
70        \e                Escape character.
71        \E                Turn off \Q, \L and \U processing.  Not in [].
72        \f                Form feed.
73        \g{}, \g1         Named, absolute or relative backreference.  Not in [].
74        \G                Pos assertion.  Not in [].
75        \h                Character class for horizontal whitespace.
76        \H                Character class for non horizontal whitespace.
77        \k{}, \k<>, \k''  Named backreference.  Not in [].
78        \K                Keep the stuff left of \K.  Not in [].
79        \l                Lowercase next character.  Not in [].
80        \L                Lowercase till \E.  Not in [].
81        \n                (Logical) newline character.
82        \N                Any character but newline.  Experimental.  Not in [].
83        \N{}              Named or numbered (Unicode) character.
84        \p{}, \pP         Character with the given Unicode property.
85        \P{}, \PP         Character without the given Unicode property.
86        \Q                Quotemeta till \E.  Not in [].
87        \r                Return character.
88        \R                Generic new line.  Not in [].
89        \s                Character class for whitespace.
90        \S                Character class for non whitespace.
91        \t                Tab character.
92        \u                Titlecase next character.  Not in [].
93        \U                Uppercase till \E.  Not in [].
94        \v                Character class for vertical whitespace.
95        \V                Character class for non vertical whitespace.
96        \w                Character class for word characters.
97        \W                Character class for non-word characters.
98        \x{}, \x00        Hexadecimal escape sequence.
99        \X                Unicode "extended grapheme cluster".  Not in [].
100        \z                End of string.  Not in [].
101        \Z                End of string.  Not in [].
102
103   Character Escapes
104       Fixed characters
105
106       A handful of characters have a dedicated character escape. The
107       following table shows them, along with their ASCII code points (in
108       decimal and hex), their ASCII name, the control escape on ASCII
109       platforms and a short description.  (For EBCDIC platforms, see
110       "OPERATOR DIFFERENCES" in perlebcdic.)
111
112        Seq.  Code Point  ASCII   Cntrl   Description.
113              Dec    Hex
114         \a     7     07    BEL    \cG    alarm or bell
115         \b     8     08     BS    \cH    backspace [1]
116         \e    27     1B    ESC    \c[    escape character
117         \f    12     0C     FF    \cL    form feed
118         \n    10     0A     LF    \cJ    line feed [2]
119         \r    13     0D     CR    \cM    carriage return
120         \t     9     09    TAB    \cI    tab
121
122       [1] "\b" is the backspace character only inside a character class.
123           Outside a character class, "\b" is a word/non-word boundary.
124
125       [2] "\n" matches a logical newline. Perl will convert between "\n" and
126           your OS's native newline character when reading from or writing to
127           text files.
128
129       Example
130
131        $str =~ /\t/;   # Matches if $str contains a (horizontal) tab.
132
133       Control characters
134
135       "\c" is used to denote a control character; the character following
136       "\c" determines the value of the construct.  For example the value of
137       "\cA" is chr(1), and the value of "\cb" is chr(2), etc.  The gory
138       details are in "Regexp Quote-Like Operators" in perlop.  A complete
139       list of what chr(1), etc. means for ASCII and EBCDIC platforms is in
140       "OPERATOR DIFFERENCES" in perlebcdic.
141
142       Note that "\c\" alone at the end of a regular expression (or doubled-
143       quoted string) is not valid.  The backslash must be followed by another
144       character.  That is, "\c\X" means "chr(28) . 'X'" for all characters X.
145
146       To write platform-independent code, you must use "\N{NAME}" instead,
147       like "\N{ESCAPE}" or "\N{U+001B}", see charnames.
148
149       Mnemonic: control character.
150
151       Example
152
153        $str =~ /\cK/;  # Matches if $str contains a vertical tab (control-K).
154
155       Named or numbered characters
156
157       Unicode characters have a Unicode name and numeric ordinal value.  Use
158       the "\N{}" construct to specify a character by either of these values.
159
160       To specify by name, the name of the character goes between the curly
161       braces.  In this case, you have to "use charnames" to load the Unicode
162       names of the characters, otherwise Perl will complain.
163
164       To specify by Unicode ordinal number, use the form "\N{U+wide hex
165       character}", where wide hex character is a number in hexadecimal that
166       gives the ordinal number that Unicode has assigned to the desired
167       character.  It is customary (but not required) to use leading zeros to
168       pad the number to 4 digits.  Thus "\N{U+0041}" means "Latin Capital
169       Letter A", and you will rarely see it written without the two leading
170       zeros.  "\N{U+0041}" means "A" even on EBCDIC machines (where the
171       ordinal value of "A" is not 0x41).
172
173       It is even possible to give your own names to characters, and even to
174       short sequences of characters.  For details, see charnames.
175
176       (There is an expanded internal form that you may see in debug output:
177       "\N{U+wide hex character.wide hex character...}".  The "..." means any
178       number of these wide hex characters separated by dots.  This represents
179       the sequence formed by the characters.  This is an internal form only,
180       subject to change, and you should not try to use it yourself.)
181
182       Mnemonic: Named character.
183
184       Note that a character that is expressed as a named or numbered
185       character is considered as a character without special meaning by the
186       regex engine, and will match "as is".
187
188       Example
189
190        use charnames ':full';               # Loads the Unicode names.
191        $str =~ /\N{THAI CHARACTER SO SO}/;  # Matches the Thai SO SO character
192
193        use charnames 'Cyrillic';            # Loads Cyrillic names.
194        $str =~ /\N{ZHE}\N{KA}/;             # Match "ZHE" followed by "KA".
195
196       Octal escapes
197
198       Octal escapes consist of a backslash followed by two or three octal
199       digits matching the code point of the character you want to use. This
200       allows for 512 characters ("\00" up to "\777") that can be expressed
201       this way (but anything above "\377" is deprecated).  Enough in pre-
202       Unicode days, but most Unicode characters cannot be escaped this way.
203
204       Note that a character that is expressed as an octal escape is
205       considered as a character without special meaning by the regex engine,
206       and will match "as is".
207
208       Examples (assuming an ASCII platform)
209
210        $str = "Perl";
211        $str =~ /\120/;    # Match, "\120" is "P".
212        $str =~ /\120+/;   # Match, "\120" is "P", it is repeated at least once.
213        $str =~ /P\053/;   # No match, "\053" is "+" and taken literally.
214
215       Caveat
216
217       Octal escapes potentially clash with backreferences. They both consist
218       of a backslash followed by numbers. So Perl has to use heuristics to
219       determine whether it is a backreference or an octal escape. Perl uses
220       the following rules:
221
222       1.  If the backslash is followed by a single digit, it's a
223           backreference.
224
225       2.  If the first digit following the backslash is a 0, it's an octal
226           escape.
227
228       3.  If the number following the backslash is N (in decimal), and Perl
229           already has seen N capture groups, Perl will consider this to be a
230           backreference.  Otherwise, it will consider it to be an octal
231           escape. Note that if N has more than three digits, Perl only takes
232           the first three for the octal escape; the rest are matched as is.
233
234            my $pat  = "(" x 999;
235               $pat .= "a";
236               $pat .= ")" x 999;
237            /^($pat)\1000$/;   #  Matches 'aa'; there are 1000 capture groups.
238            /^$pat\1000$/;     #  Matches 'a@0'; there are 999 capture groups
239                               #    and \1000 is seen as \100 (a '@') and a '0'.
240
241       Hexadecimal escapes
242
243       Hexadecimal escapes start with "\x" and are then either followed by a
244       two digit hexadecimal number, or a hexadecimal number of arbitrary
245       length surrounded by curly braces. The hexadecimal number is the code
246       point of the character you want to express.
247
248       Note that a character that is expressed as a hexadecimal escape is
249       considered as a character without special meaning by the regex engine,
250       and will match "as is".
251
252       Mnemonic: hexadecimal.
253
254       Examples (assuming an ASCII platform)
255
256        $str = "Perl";
257        $str =~ /\x50/;    # Match, "\x50" is "P".
258        $str =~ /\x50+/;   # Match, "\x50" is "P", it is repeated at least once.
259        $str =~ /P\x2B/;   # No match, "\x2B" is "+" and taken literally.
260
261        /\x{2603}\x{2602}/ # Snowman with an umbrella.
262                           # The Unicode character 2603 is a snowman,
263                           # the Unicode character 2602 is an umbrella.
264        /\x{263B}/         # Black smiling face.
265        /\x{263b}/         # Same, the hex digits A - F are case insensitive.
266
267   Modifiers
268       A number of backslash sequences have to do with changing the character,
269       or characters following them. "\l" will lowercase the character
270       following it, while "\u" will uppercase (or, more accurately,
271       titlecase) the character following it. (They perform similar
272       functionality as the functions "lcfirst" and "ucfirst").
273
274       To uppercase or lowercase several characters, one might want to use
275       "\L" or "\U", which will lowercase/uppercase all characters following
276       them, until either the end of the pattern, or the next occurrence of
277       "\E", whatever comes first. They perform similar functionality as the
278       functions "lc" and "uc" do.
279
280       "\Q" is used to escape all characters following, up to the next "\E" or
281       the end of the pattern. "\Q" adds a backslash to any character that
282       isn't a letter, digit or underscore. This will ensure that any
283       character between "\Q" and "\E" is matched literally, and will not be
284       interpreted by the regexp engine.
285
286       Mnemonic: Lowercase, Uppercase, Quotemeta, End.
287
288       Examples
289
290        $sid     = "sid";
291        $greg    = "GrEg";
292        $miranda = "(Miranda)";
293        $str     =~ /\u$sid/;        # Matches 'Sid'
294        $str     =~ /\L$greg/;       # Matches 'greg'
295        $str     =~ /\Q$miranda\E/;  # Matches '(Miranda)', as if the pattern
296                                     #   had been written as /\(Miranda\)/
297
298   Character classes
299       Perl regular expressions have a large range of character classes. Some
300       of the character classes are written as a backslash sequence. We will
301       briefly discuss those here; full details of character classes can be
302       found in perlrecharclass.
303
304       "\w" is a character class that matches any single word character
305       (letters, digits, underscore). "\d" is a character class that matches
306       any decimal digit, while the character class "\s" matches any
307       whitespace character.  New in perl 5.10.0 are the classes "\h" and "\v"
308       which match horizontal and vertical whitespace characters.
309
310       The uppercase variants ("\W", "\D", "\S", "\H", and "\V") are character
311       classes that match any character that isn't a word character, digit,
312       whitespace, horizontal whitespace nor vertical whitespace.
313
314       Mnemonics: word, digit, space, horizontal, vertical.
315
316       Unicode classes
317
318       "\pP" (where "P" is a single letter) and "\p{Property}" are used to
319       match a character that matches the given Unicode property; properties
320       include things like "letter", or "thai character". Capitalizing the
321       sequence to "\PP" and "\P{Property}" make the sequence match a
322       character that doesn't match the given Unicode property. For more
323       details, see "Backslash sequences" in perlrecharclass and "Unicode
324       Character Properties" in perlunicode.
325
326       Mnemonic: property.
327
328   Referencing
329       If capturing parenthesis are used in a regular expression, we can refer
330       to the part of the source string that was matched, and match exactly
331       the same thing. There are three ways of referring to such
332       backreference: absolutely, relatively, and by name.
333
334       Absolute referencing
335
336       A backslash sequence that starts with a backslash and is followed by a
337       number is an absolute reference (but be aware of the caveat mentioned
338       above).  If the number is N, it refers to the Nth set of parentheses -
339       whatever has been matched by that set of parenthesis has to be matched
340       by the "\N" as well.
341
342       Examples
343
344        /(\w+) \1/;    # Finds a duplicated word, (e.g. "cat cat").
345        /(.)(.)\2\1/;  # Match a four letter palindrome (e.g. "ABBA").
346
347       Relative referencing
348
349       New in perl 5.10.0 is a different way of referring to capture buffers:
350       "\g".  "\g" takes a number as argument, with the number in curly braces
351       (the braces are optional). If the number (N) does not have a sign, it's
352       a reference to the Nth capture group (so "\g{2}" is equivalent to "\2"
353       - except that "\g" always refers to a capture group and will never be
354       seen as an octal escape). If the number is negative, the reference is
355       relative, referring to the Nth group before the "\g{-N}".
356
357       The big advantage of "\g{-N}" is that it makes it much easier to write
358       patterns with references that can be interpolated in larger patterns,
359       even if the larger pattern also contains capture groups.
360
361       Mnemonic: group.
362
363       Examples
364
365        /(A)        # Buffer 1
366         (          # Buffer 2
367           (B)      # Buffer 3
368           \g{-1}   # Refers to buffer 3 (B)
369           \g{-3}   # Refers to buffer 1 (A)
370         )
371        /x;         # Matches "ABBA".
372
373        my $qr = qr /(.)(.)\g{-2}\g{-1}/;  # Matches 'abab', 'cdcd', etc.
374        /$qr$qr/                           # Matches 'ababcdcd'.
375
376       Named referencing
377
378       Also new in perl 5.10.0 is the use of named capture buffers, which can
379       be referred to by name. This is done with "\g{name}", which is a
380       backreference to the capture buffer with the name name.
381
382       To be compatible with .Net regular expressions, "\g{name}" may also be
383       written as "\k{name}", "\k<name>" or "\k'name'".
384
385       Note that "\g{}" has the potential to be ambiguous, as it could be a
386       named reference, or an absolute or relative reference (if its argument
387       is numeric).  However, names are not allowed to start with digits, nor
388       are they allowed to contain a hyphen, so there is no ambiguity.
389
390       Examples
391
392        /(?<word>\w+) \g{word}/ # Finds duplicated word, (e.g. "cat cat")
393        /(?<word>\w+) \k{word}/ # Same.
394        /(?<word>\w+) \k<word>/ # Same.
395        /(?<letter1>.)(?<letter2>.)\g{letter2}\g{letter1}/
396                                # Match a four letter palindrome (e.g. "ABBA")
397
398   Assertions
399       Assertions are conditions that have to be true; they don't actually
400       match parts of the substring. There are six assertions that are written
401       as backslash sequences.
402
403       \A  "\A" only matches at the beginning of the string. If the "/m"
404           modifier isn't used, then "/\A/" is equivalent with "/^/". However,
405           if the "/m" modifier is used, then "/^/" matches internal newlines,
406           but the meaning of "/\A/" isn't changed by the "/m" modifier. "\A"
407           matches at the beginning of the string regardless whether the "/m"
408           modifier is used.
409
410       \z, \Z
411           "\z" and "\Z" match at the end of the string. If the "/m" modifier
412           isn't used, then "/\Z/" is equivalent with "/$/", that is, it
413           matches at the end of the string, or before the newline at the end
414           of the string. If the "/m" modifier is used, then "/$/" matches at
415           internal newlines, but the meaning of "/\Z/" isn't changed by the
416           "/m" modifier. "\Z" matches at the end of the string (or just
417           before a trailing newline) regardless whether the "/m" modifier is
418           used.
419
420           "\z" is just like "\Z", except that it will not match before a
421           trailing newline. "\z" will only match at the end of the string -
422           regardless of the modifiers used, and not before a newline.
423
424       \G  "\G" is usually only used in combination with the "/g" modifier. If
425           the "/g" modifier is used (and the match is done in scalar
426           context), Perl will remember where in the source string the last
427           match ended, and the next time, it will start the match from where
428           it ended the previous time.
429
430           "\G" matches the point where the previous match ended, or the
431           beginning of the string if there was no previous match.
432
433           Mnemonic: Global.
434
435       \b, \B
436           "\b" matches at any place between a word and a non-word character;
437           "\B" matches at any place between characters where "\b" doesn't
438           match. "\b" and "\B" assume there's a non-word character before the
439           beginning and after the end of the source string; so "\b" will
440           match at the beginning (or end) of the source string if the source
441           string begins (or ends) with a word character. Otherwise, "\B" will
442           match.
443
444           Mnemonic: boundary.
445
446       Examples
447
448         "cat"   =~ /\Acat/;     # Match.
449         "cat"   =~ /cat\Z/;     # Match.
450         "cat\n" =~ /cat\Z/;     # Match.
451         "cat\n" =~ /cat\z/;     # No match.
452
453         "cat"   =~ /\bcat\b/;   # Matches.
454         "cats"  =~ /\bcat\b/;   # No match.
455         "cat"   =~ /\bcat\B/;   # No match.
456         "cats"  =~ /\bcat\B/;   # Match.
457
458         while ("cat dog" =~ /(\w+)/g) {
459             print $1;           # Prints 'catdog'
460         }
461         while ("cat dog" =~ /\G(\w+)/g) {
462             print $1;           # Prints 'cat'
463         }
464
465   Misc
466       Here we document the backslash sequences that don't fall in one of the
467       categories above. They are:
468
469       \C  "\C" always matches a single octet, even if the source string is
470           encoded in UTF-8 format, and the character to be matched is a
471           multi-octet character.  "\C" was introduced in perl 5.6.
472
473           Mnemonic: oCtet.
474
475       \K  This is new in perl 5.10.0. Anything that is matched left of "\K"
476           is not included in $& - and will not be replaced if the pattern is
477           used in a substitution. This will allow you to write "s/PAT1 \K
478           PAT2/REPL/x" instead of "s/(PAT1) PAT2/${1}REPL/x" or "s/(?<=PAT1)
479           PAT2/REPL/x".
480
481           Mnemonic: Keep.
482
483       \N  This is a new experimental feature in perl 5.12.0.  It matches any
484           character that is not a newline.  It is a short-hand for writing
485           "[^\n]", and is identical to the "." metasymbol, except under the
486           "/s" flag, which changes the meaning of ".", but not "\N".
487
488           Note that "\N{...}" can mean a named or numbered character.
489
490           Mnemonic: Complement of \n.
491
492       \R  "\R" matches a generic newline, that is, anything that is
493           considered a newline by Unicode. This includes all characters
494           matched by "\v" (vertical whitespace), and the multi character
495           sequence "\x0D\x0A" (carriage return followed by a line feed, aka
496           the network newline, or the newline used in Windows text files).
497           "\R" is equivalent to "(?>\x0D\x0A)|\v)". Since "\R" can match a
498           sequence of more than one character, it cannot be put inside a
499           bracketed character class; "/[\R]/" is an error; use "\v" instead.
500           "\R" was introduced in perl 5.10.0.
501
502           Mnemonic: none really. "\R" was picked because PCRE already uses
503           "\R", and more importantly because Unicode recommends such a
504           regular expression metacharacter, and suggests "\R" as the
505           notation.
506
507       \X  This matches a Unicode extended grapheme cluster.
508
509           "\X" matches quite well what normal (non-Unicode-programmer) usage
510           would consider a single character.  As an example, consider a G
511           with some sort of diacritic mark, such as an arrow.  There is no
512           such single character in Unicode, but one can be composed by using
513           a G followed by a Unicode "COMBINING UPWARDS ARROW BELOW", and
514           would be displayed by Unicode-aware software as if it were a single
515           character.
516
517           Mnemonic: eXtended Unicode character.
518
519       Examples
520
521        "\x{256}" =~ /^\C\C$/;    # Match as chr (256) takes 2 octets in UTF-8.
522
523        $str =~ s/foo\Kbar/baz/g; # Change any 'bar' following a 'foo' to 'baz'.
524        $str =~ s/(.)\K\1//g;     # Delete duplicated characters.
525
526        "\n"   =~ /^\R$/;         # Match, \n   is a generic newline.
527        "\r"   =~ /^\R$/;         # Match, \r   is a generic newline.
528        "\r\n" =~ /^\R$/;         # Match, \r\n is a generic newline.
529
530        "P\x{0307}" =~ /^\X$/     # \X matches a P with a dot above.
531
532
533
534perl v5.12.4                      2011-06-07                PERLREBACKSLASH(1)
Impressum