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