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
31       the backslash just takes away any special meaning 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
39       issues 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 alphanumeric character as the
51           delimiter of your pattern (which you probably shouldn't do for
52           readability reasons), you have to escape the delimiter if you want
53           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.  See also \o{}.
61        \1                Absolute backreference.  Not in [].
62        \a                Alarm or bell.
63        \A                Beginning of string.  Not in [].
64        \b{}, \b          Boundary. (\b is a backspace in []).
65        \B{}, \B          Not a boundary.  Not in [].
66        \cX               Control-X.
67        \d                Match any digit character.
68        \D                Match any character that isn't a digit.
69        \e                Escape character.
70        \E                Turn off \Q, \L and \U processing.  Not in [].
71        \f                Form feed.
72        \F                Foldcase till \E.  Not in [].
73        \g{}, \g1         Named, absolute or relative backreference.
74                          Not in [].
75        \G                Pos assertion.  Not in [].
76        \h                Match any horizontal whitespace character.
77        \H                Match any character that isn't horizontal whitespace.
78        \k{}, \k<>, \k''  Named backreference.  Not in [].
79        \K                Keep the stuff left of \K.  Not in [].
80        \l                Lowercase next character.  Not in [].
81        \L                Lowercase till \E.  Not in [].
82        \n                (Logical) newline character.
83        \N                Match any character but newline.  Not in [].
84        \N{}              Named or numbered (Unicode) character or sequence.
85        \o{}              Octal escape sequence.
86        \p{}, \pP         Match any character with the given Unicode property.
87        \P{}, \PP         Match any character without the given property.
88        \Q                Quote (disable) pattern metacharacters till \E.  Not
89                          in [].
90        \r                Return character.
91        \R                Generic new line.  Not in [].
92        \s                Match any whitespace character.
93        \S                Match any character that isn't a whitespace.
94        \t                Tab character.
95        \u                Titlecase next character.  Not in [].
96        \U                Uppercase till \E.  Not in [].
97        \v                Match any vertical whitespace character.
98        \V                Match any character that isn't vertical whitespace
99        \w                Match any word character.
100        \W                Match any character that isn't a word character.
101        \x{}, \x00        Hexadecimal escape sequence.
102        \X                Unicode "extended grapheme cluster".  Not in [].
103        \z                End of string.  Not in [].
104        \Z                End of string.  Not in [].
105
106   Character Escapes
107       Fixed characters
108
109       A handful of characters have a dedicated character escape. The
110       following table shows them, along with their ASCII code points (in
111       decimal and hex), their ASCII name, the control escape on ASCII
112       platforms and a short description.  (For EBCDIC platforms, see
113       "OPERATOR DIFFERENCES" in perlebcdic.)
114
115        Seq.  Code Point  ASCII   Cntrl   Description.
116              Dec    Hex
117         \a     7     07    BEL    \cG    alarm or bell
118         \b     8     08     BS    \cH    backspace [1]
119         \e    27     1B    ESC    \c[    escape character
120         \f    12     0C     FF    \cL    form feed
121         \n    10     0A     LF    \cJ    line feed [2]
122         \r    13     0D     CR    \cM    carriage return
123         \t     9     09    TAB    \cI    tab
124
125       [1] "\b" is the backspace character only inside a character class.
126           Outside a character class, "\b" alone is a
127           word-character/non-word-character boundary, and "\b{}" is some
128           other type of boundary.
129
130       [2] "\n" matches a logical newline. Perl converts between "\n" and your
131           OS's native newline character when reading from or writing to text
132           files.
133
134       Example
135
136        $str =~ /\t/;   # Matches if $str contains a (horizontal) tab.
137
138       Control characters
139
140       "\c" is used to denote a control character; the character following
141       "\c" determines the value of the construct.  For example the value of
142       "\cA" is chr(1), and the value of "\cb" is chr(2), etc.  The gory
143       details are in "Regexp Quote-Like Operators" in perlop.  A complete
144       list of what chr(1), etc. means for ASCII and EBCDIC platforms is in
145       "OPERATOR DIFFERENCES" in perlebcdic.
146
147       Note that "\c\" alone at the end of a regular expression (or doubled-
148       quoted string) is not valid.  The backslash must be followed by another
149       character.  That is, "\c\X" means "chr(28) . 'X'" for all characters X.
150
151       To write platform-independent code, you must use "\N{NAME}" instead,
152       like "\N{ESCAPE}" or "\N{U+001B}", see charnames.
153
154       Mnemonic: control character.
155
156       Example
157
158        $str =~ /\cK/;  # Matches if $str contains a vertical tab (control-K).
159
160       Named or numbered characters and character sequences
161
162       Unicode characters have a Unicode name and numeric code point (ordinal)
163       value.  Use the "\N{}" construct to specify a character by either of
164       these values.  Certain sequences of characters also have names.
165
166       To specify by name, the name of the character or character sequence
167       goes between the curly braces.
168
169       To specify a character by Unicode code point, use the form "\N{U+code
170       point}", where code point is a number in hexadecimal that gives the
171       code point that Unicode has assigned to the desired character.  It is
172       customary but not required to use leading zeros to pad the number to 4
173       digits.  Thus "\N{U+0041}" means "LATIN CAPITAL LETTER A", and you will
174       rarely see it written without the two leading zeros.  "\N{U+0041}"
175       means "A" even on EBCDIC machines (where the ordinal value of "A" is
176       not 0x41).
177
178       It is even possible to give your own names to characters and character
179       sequences.  For details, see charnames.
180
181       (There is an expanded internal form that you may see in debug output:
182       "\N{U+code point.code point...}".  The "..." means any number of these
183       code points separated by dots.  This represents the sequence formed by
184       the characters.  This is an internal form only, subject to change, and
185       you should not try to use it yourself.)
186
187       Mnemonic: Named character.
188
189       Note that a character or character sequence expressed as a named or
190       numbered character is considered a character without special meaning by
191       the regex engine, and will match "as is".
192
193       Example
194
195        $str =~ /\N{THAI CHARACTER SO SO}/;  # Matches the Thai SO SO character
196
197        use charnames 'Cyrillic';            # Loads Cyrillic names.
198        $str =~ /\N{ZHE}\N{KA}/;             # Match "ZHE" followed by "KA".
199
200       Octal escapes
201
202       There are two forms of octal escapes.  Each is used to specify a
203       character by its code point specified in octal notation.
204
205       One form, available starting in Perl 5.14 looks like "\o{...}", where
206       the dots represent one or more octal digits.  It can be used for any
207       Unicode character.
208
209       It was introduced to avoid the potential problems with the other form,
210       available in all Perls.  That form consists of a backslash followed by
211       three octal digits.  One problem with this form is that it can look
212       exactly like an old-style backreference (see "Disambiguation rules
213       between old-style octal escapes and backreferences" below.)  You can
214       avoid this by making the first of the three digits always a zero, but
215       that makes \077 the largest code point specifiable.
216
217       In some contexts, a backslash followed by two or even one octal digits
218       may be interpreted as an octal escape, sometimes with a warning, and
219       because of some bugs, sometimes with surprising results.  Also, if you
220       are creating a regex out of smaller snippets concatenated together, and
221       you use fewer than three digits, the beginning of one snippet may be
222       interpreted as adding digits to the ending of the snippet before it.
223       See "Absolute referencing" for more discussion and examples of the
224       snippet problem.
225
226       Note that a character expressed as an octal escape is considered a
227       character without special meaning by the regex engine, and will match
228       "as is".
229
230       To summarize, the "\o{}" form is always safe to use, and the other form
231       is safe to use for code points through \077 when you use exactly three
232       digits to specify them.
233
234       Mnemonic: 0ctal or octal.
235
236       Examples (assuming an ASCII platform)
237
238        $str = "Perl";
239        $str =~ /\o{120}/;  # Match, "\120" is "P".
240        $str =~ /\120/;     # Same.
241        $str =~ /\o{120}+/; # Match, "\120" is "P",
242                            # it's repeated at least once.
243        $str =~ /\120+/;    # Same.
244        $str =~ /P\053/;    # No match, "\053" is "+" and taken literally.
245        /\o{23073}/         # Black foreground, white background smiling face.
246        /\o{4801234567}/    # Raises a warning, and yields chr(4).
247
248       Disambiguation rules between old-style octal escapes and backreferences
249
250       Octal escapes of the "\000" form outside of bracketed character classes
251       potentially clash with old-style backreferences (see "Absolute
252       referencing" below).  They both consist of a backslash followed by
253       numbers.  So Perl has to use heuristics to determine whether it is a
254       backreference or an octal escape.  Perl uses the following rules to
255       disambiguate:
256
257       1.  If the backslash is followed by a single digit, it's a
258           backreference.
259
260       2.  If the first digit following the backslash is a 0, it's an octal
261           escape.
262
263       3.  If the number following the backslash is N (in decimal), and Perl
264           already has seen N capture groups, Perl considers this a
265           backreference.  Otherwise, it considers it an octal escape. If N
266           has more than three digits, Perl takes only the first three for the
267           octal escape; the rest are matched as is.
268
269            my $pat  = "(" x 999;
270               $pat .= "a";
271               $pat .= ")" x 999;
272            /^($pat)\1000$/;   #  Matches 'aa'; there are 1000 capture groups.
273            /^$pat\1000$/;     #  Matches 'a@0'; there are 999 capture groups
274                               #  and \1000 is seen as \100 (a '@') and a '0'.
275
276       You can force a backreference interpretation always by using the
277       "\g{...}" form.  You can the force an octal interpretation always by
278       using the "\o{...}" form, or for numbers up through \077 (= 63
279       decimal), by using three digits, beginning with a "0".
280
281       Hexadecimal escapes
282
283       Like octal escapes, there are two forms of hexadecimal escapes, but
284       both start with the sequence "\x".  This is followed by either exactly
285       two hexadecimal digits forming a number, or a hexadecimal number of
286       arbitrary length surrounded by curly braces. The hexadecimal number is
287       the code point of the character you want to express.
288
289       Note that a character expressed as one of these escapes is considered a
290       character without special meaning by the regex engine, and will match
291       "as is".
292
293       Mnemonic: hexadecimal.
294
295       Examples (assuming an ASCII platform)
296
297        $str = "Perl";
298        $str =~ /\x50/;    # Match, "\x50" is "P".
299        $str =~ /\x50+/;   # Match, "\x50" is "P", it is repeated at least once
300        $str =~ /P\x2B/;   # No match, "\x2B" is "+" and taken literally.
301
302        /\x{2603}\x{2602}/ # Snowman with an umbrella.
303                           # The Unicode character 2603 is a snowman,
304                           # the Unicode character 2602 is an umbrella.
305        /\x{263B}/         # Black smiling face.
306        /\x{263b}/         # Same, the hex digits A - F are case insensitive.
307
308   Modifiers
309       A number of backslash sequences have to do with changing the character,
310       or characters following them. "\l" will lowercase the character
311       following it, while "\u" will uppercase (or, more accurately,
312       titlecase) the character following it. They provide functionality
313       similar to the functions "lcfirst" and "ucfirst".
314
315       To uppercase or lowercase several characters, one might want to use
316       "\L" or "\U", which will lowercase/uppercase all characters following
317       them, until either the end of the pattern or the next occurrence of
318       "\E", whichever comes first. They provide functionality similar to what
319       the functions "lc" and "uc" provide.
320
321       "\Q" is used to quote (disable) pattern metacharacters, up to the next
322       "\E" or the end of the pattern. "\Q" adds a backslash to any character
323       that could have special meaning to Perl.  In the ASCII range, it quotes
324       every character that isn't a letter, digit, or underscore.  See
325       "quotemeta" in perlfunc for details on what gets quoted for non-ASCII
326       code points.  Using this ensures that any character between "\Q" and
327       "\E" will be matched literally, not interpreted as a metacharacter by
328       the regex engine.
329
330       "\F" can be used to casefold all characters following, up to the next
331       "\E" or the end of the pattern. It provides the functionality similar
332       to the "fc" function.
333
334       Mnemonic: Lowercase, Uppercase, Fold-case, Quotemeta, End.
335
336       Examples
337
338        $sid     = "sid";
339        $greg    = "GrEg";
340        $miranda = "(Miranda)";
341        $str     =~ /\u$sid/;        # Matches 'Sid'
342        $str     =~ /\L$greg/;       # Matches 'greg'
343        $str     =~ /\Q$miranda\E/;  # Matches '(Miranda)', as if the pattern
344                                     #   had been written as /\(Miranda\)/
345
346   Character classes
347       Perl regular expressions have a large range of character classes. Some
348       of the character classes are written as a backslash sequence. We will
349       briefly discuss those here; full details of character classes can be
350       found in perlrecharclass.
351
352       "\w" is a character class that matches any single word character
353       (letters, digits, Unicode marks, and connector punctuation (like the
354       underscore)).  "\d" is a character class that matches any decimal
355       digit, while the character class "\s" matches any whitespace character.
356       New in perl 5.10.0 are the classes "\h" and "\v" which match horizontal
357       and vertical whitespace characters.
358
359       The exact set of characters matched by "\d", "\s", and "\w" varies
360       depending on various pragma and regular expression modifiers.  It is
361       possible to restrict the match to the ASCII range by using the "/a"
362       regular expression modifier.  See perlrecharclass.
363
364       The uppercase variants ("\W", "\D", "\S", "\H", and "\V") are character
365       classes that match, respectively, any character that isn't a word
366       character, digit, whitespace, horizontal whitespace, or vertical
367       whitespace.
368
369       Mnemonics: word, digit, space, horizontal, vertical.
370
371       Unicode classes
372
373       "\pP" (where "P" is a single letter) and "\p{Property}" are used to
374       match a character that matches the given Unicode property; properties
375       include things like "letter", or "thai character". Capitalizing the
376       sequence to "\PP" and "\P{Property}" make the sequence match a
377       character that doesn't match the given Unicode property. For more
378       details, see "Backslash sequences" in perlrecharclass and "Unicode
379       Character Properties" in perlunicode.
380
381       Mnemonic: property.
382
383   Referencing
384       If capturing parenthesis are used in a regular expression, we can refer
385       to the part of the source string that was matched, and match exactly
386       the same thing. There are three ways of referring to such
387       backreference: absolutely, relatively, and by name.
388
389       Absolute referencing
390
391       Either "\gN" (starting in Perl 5.10.0), or "\N" (old-style) where N is
392       a positive (unsigned) decimal number of any length is an absolute
393       reference to a capturing group.
394
395       N refers to the Nth set of parentheses, so "\gN" refers to whatever has
396       been matched by that set of parentheses.  Thus "\g1" refers to the
397       first capture group in the regex.
398
399       The "\gN" form can be equivalently written as "\g{N}" which avoids
400       ambiguity when building a regex by concatenating shorter strings.
401       Otherwise if you had a regex "qr/$a$b/", and $a contained "\g1", and $b
402       contained "37", you would get "/\g137/" which is probably not what you
403       intended.
404
405       In the "\N" form, N must not begin with a "0", and there must be at
406       least N capturing groups, or else N is considered an octal escape (but
407       something like "\18" is the same as "\0018"; that is, the octal escape
408       "\001" followed by a literal digit "8").
409
410       Mnemonic: group.
411
412       Examples
413
414        /(\w+) \g1/;    # Finds a duplicated word, (e.g. "cat cat").
415        /(\w+) \1/;     # Same thing; written old-style.
416        /(.)(.)\g2\g1/;  # Match a four letter palindrome (e.g. "ABBA").
417
418       Relative referencing
419
420       "\g-N" (starting in Perl 5.10.0) is used for relative addressing.  (It
421       can be written as "\g{-N".)  It refers to the Nth group before the
422       "\g{-N}".
423
424       The big advantage of this form is that it makes it much easier to write
425       patterns with references that can be interpolated in larger patterns,
426       even if the larger pattern also contains capture groups.
427
428       Examples
429
430        /(A)        # Group 1
431         (          # Group 2
432           (B)      # Group 3
433           \g{-1}   # Refers to group 3 (B)
434           \g{-3}   # Refers to group 1 (A)
435         )
436        /x;         # Matches "ABBA".
437
438        my $qr = qr /(.)(.)\g{-2}\g{-1}/;  # Matches 'abab', 'cdcd', etc.
439        /$qr$qr/                           # Matches 'ababcdcd'.
440
441       Named referencing
442
443       "\g{name}" (starting in Perl 5.10.0) can be used to back refer to a
444       named capture group, dispensing completely with having to think about
445       capture buffer positions.
446
447       To be compatible with .Net regular expressions, "\g{name}" may also be
448       written as "\k{name}", "\k<name>" or "\k'name'".
449
450       To prevent any ambiguity, name must not start with a digit nor contain
451       a hyphen.
452
453       Examples
454
455        /(?<word>\w+) \g{word}/ # Finds duplicated word, (e.g. "cat cat")
456        /(?<word>\w+) \k{word}/ # Same.
457        /(?<word>\w+) \k<word>/ # Same.
458        /(?<letter1>.)(?<letter2>.)\g{letter2}\g{letter1}/
459                                # Match a four letter palindrome (e.g. "ABBA")
460
461   Assertions
462       Assertions are conditions that have to be true; they don't actually
463       match parts of the substring. There are six assertions that are written
464       as backslash sequences.
465
466       \A  "\A" only matches at the beginning of the string. If the "/m"
467           modifier isn't used, then "/\A/" is equivalent to "/^/". However,
468           if the "/m" modifier is used, then "/^/" matches internal newlines,
469           but the meaning of "/\A/" isn't changed by the "/m" modifier. "\A"
470           matches at the beginning of the string regardless whether the "/m"
471           modifier is used.
472
473       \z, \Z
474           "\z" and "\Z" match at the end of the string. If the "/m" modifier
475           isn't used, then "/\Z/" is equivalent to "/$/"; that is, it matches
476           at the end of the string, or one before the newline at the end of
477           the string. If the "/m" modifier is used, then "/$/" matches at
478           internal newlines, but the meaning of "/\Z/" isn't changed by the
479           "/m" modifier. "\Z" matches at the end of the string (or just
480           before a trailing newline) regardless whether the "/m" modifier is
481           used.
482
483           "\z" is just like "\Z", except that it does not match before a
484           trailing newline. "\z" matches at the end of the string only,
485           regardless of the modifiers used, and not just before a newline.
486           It is how to anchor the match to the true end of the string under
487           all conditions.
488
489       \G  "\G" is usually used only in combination with the "/g" modifier. If
490           the "/g" modifier is used and the match is done in scalar context,
491           Perl remembers where in the source string the last match ended, and
492           the next time, it will start the match from where it ended the
493           previous time.
494
495           "\G" matches the point where the previous match on that string
496           ended, or the beginning of that string if there was no previous
497           match.
498
499           Mnemonic: Global.
500
501       \b{}, \b, \B{}, \B
502           "\b{...}", available starting in v5.22, matches a boundary (between
503           two characters, or before the first character of the string, or
504           after the final character of the string) based on the Unicode rules
505           for the boundary type specified inside the braces.  The boundary
506           types are given a few paragraphs below.  "\B{...}" matches at any
507           place between characters where "\b{...}" of the same type doesn't
508           match.
509
510           "\b" when not immediately followed by a "{" matches at any place
511           between a word (something matched by "\w") and a non-word character
512           ("\W"); "\B" when not immediately followed by a "{" matches at any
513           place between characters where "\b" doesn't match.  To get better
514           word matching of natural language text, see "\b{wb}" below.
515
516           "\b" and "\B" assume there's a non-word character before the
517           beginning and after the end of the source string; so "\b" will
518           match at the beginning (or end) of the source string if the source
519           string begins (or ends) with a word character. Otherwise, "\B" will
520           match.
521
522           Do not use something like "\b=head\d\b" and expect it to match the
523           beginning of a line.  It can't, because for there to be a boundary
524           before the non-word "=", there must be a word character immediately
525           previous.  All plain "\b" and "\B" boundary determinations look for
526           word characters alone, not for non-word characters nor for string
527           ends.  It may help to understand how "\b" and "\B" work by equating
528           them as follows:
529
530               \b  really means    (?:(?<=\w)(?!\w)|(?<!\w)(?=\w))
531               \B  really means    (?:(?<=\w)(?=\w)|(?<!\w)(?!\w))
532
533           In contrast, "\b{...}" and "\B{...}" may or may not match at the
534           beginning and end of the line, depending on the boundary type.
535           These implement the Unicode default boundaries, specified in
536           <http://www.unicode.org/reports/tr14/> and
537           <http://www.unicode.org/reports/tr29/>.  The boundary types are:
538
539           "\b{gcb}" or "\b{g}"
540               This matches a Unicode "Grapheme Cluster Boundary".  (Actually
541               Perl always uses the improved "extended" grapheme cluster").
542               These are explained below under ""\X"".  In fact, "\X" is
543               another way to get the same functionality.  It is equivalent to
544               "/.+?\b{gcb}/".  Use whichever is most convenient for your
545               situation.
546
547           "\b{lb}"
548               This matches according to the default Unicode Line Breaking
549               Algorithm (<http://www.unicode.org/reports/tr14/>), as
550               customized in that document (Example 7 of revision 35
551               <http://www.unicode.org/reports/tr14/tr14-35.html#Example7>)
552               for better handling of numeric expressions.
553
554               This is suitable for many purposes, but the Unicode::LineBreak
555               module is available on CPAN that provides many more features,
556               including customization.
557
558           "\b{sb}"
559               This matches a Unicode "Sentence Boundary".  This is an aid to
560               parsing natural language sentences.  It gives good, but
561               imperfect results.  For example, it thinks that "Mr. Smith" is
562               two sentences.  More details are at
563               <http://www.unicode.org/reports/tr29/>.  Note also that it
564               thinks that anything matching "\R" (except form feed and
565               vertical tab) is a sentence boundary.  "\b{sb}" works with text
566               designed for word-processors which wrap lines automatically for
567               display, but hard-coded line boundaries are considered to be
568               essentially the ends of text blocks (paragraphs really), and
569               hence the ends of sentences.  "\b{sb}" doesn't do well with
570               text containing embedded newlines, like the source text of the
571               document you are reading.  Such text needs to be preprocessed
572               to get rid of the line separators before looking for sentence
573               boundaries.  Some people view this as a bug in the Unicode
574               standard, and this behavior is quite subject to change in
575               future Perl versions.
576
577           "\b{wb}"
578               This matches a Unicode "Word Boundary", but tailored to Perl
579               expectations.  This gives better (though not perfect) results
580               for natural language processing than plain "\b" (without
581               braces) does.  For example, it understands that apostrophes can
582               be in the middle of words and that parentheses aren't (see the
583               examples below).  More details are at
584               <http://www.unicode.org/reports/tr29/>.
585
586               The current Unicode definition of a Word Boundary matches
587               between every white space character.  Perl tailors this,
588               starting in version 5.24, to generally not break up spans of
589               white space, just as plain "\b" has always functioned.  This
590               allows "\b{wb}" to be a drop-in replacement for "\b", but with
591               generally better results for natural language processing.  (The
592               exception to this tailoring is when a span of white space is
593               immediately followed by something like U+0303, COMBINING TILDE.
594               If the final space character in the span is a horizontal white
595               space, it is broken out so that it attaches instead to the
596               combining character.  To be precise, if a span of white space
597               that ends in a horizontal space has the character immediately
598               following it have any of the Word Boundary property values
599               "Extend", "Format" or "ZWJ", the boundary between the final
600               horizontal space character and the rest of the span matches
601               "\b{wb}".  In all other cases the boundary between two white
602               space characters matches "\B{wb}".)
603
604           It is important to realize when you use these Unicode boundaries,
605           that you are taking a risk that a future version of Perl which
606           contains a later version of the Unicode Standard will not work
607           precisely the same way as it did when your code was written.  These
608           rules are not considered stable and have been somewhat more subject
609           to change than the rest of the Standard.  Unicode reserves the
610           right to change them at will, and Perl reserves the right to update
611           its implementation to Unicode's new rules.  In the past, some
612           changes have been because new characters have been added to the
613           Standard which have different characteristics than all previous
614           characters, so new rules are formulated for handling them.  These
615           should not cause any backward compatibility issues.  But some
616           changes have changed the treatment of existing characters because
617           the Unicode Technical Committee has decided that the change is
618           warranted for whatever reason.  This could be to fix a bug, or
619           because they think better results are obtained with the new rule.
620
621           It is also important to realize that these are default boundary
622           definitions, and that implementations may wish to tailor the
623           results for particular purposes and locales.  For example, some
624           languages, such as Japanese and Thai, require dictionary lookup to
625           accurately determine word boundaries.
626
627           Mnemonic: boundary.
628
629       Examples
630
631         "cat"   =~ /\Acat/;     # Match.
632         "cat"   =~ /cat\Z/;     # Match.
633         "cat\n" =~ /cat\Z/;     # Match.
634         "cat\n" =~ /cat\z/;     # No match.
635
636         "cat"   =~ /\bcat\b/;   # Matches.
637         "cats"  =~ /\bcat\b/;   # No match.
638         "cat"   =~ /\bcat\B/;   # No match.
639         "cats"  =~ /\bcat\B/;   # Match.
640
641         while ("cat dog" =~ /(\w+)/g) {
642             print $1;           # Prints 'catdog'
643         }
644         while ("cat dog" =~ /\G(\w+)/g) {
645             print $1;           # Prints 'cat'
646         }
647
648         my $s = "He said, \"Is pi 3.14? (I'm not sure).\"";
649         print join("|", $s =~ m/ ( .+? \b     ) /xg), "\n";
650         print join("|", $s =~ m/ ( .+? \b{wb} ) /xg), "\n";
651        prints
652         He| |said|, "|Is| |pi| |3|.|14|? (|I|'|m| |not| |sure
653         He| |said|,| |"|Is| |pi| |3.14|?| |(|I'm| |not| |sure|)|.|"
654
655   Misc
656       Here we document the backslash sequences that don't fall in one of the
657       categories above. These are:
658
659       \K  This appeared in perl 5.10.0. Anything matched left of "\K" is not
660           included in $&, and will not be replaced if the pattern is used in
661           a substitution. This lets you write "s/PAT1 \K PAT2/REPL/x" instead
662           of "s/(PAT1) PAT2/${1}REPL/x" or "s/(?<=PAT1) PAT2/REPL/x".
663
664           Mnemonic: Keep.
665
666       \N  This feature, available starting in v5.12,  matches any character
667           that is not a newline.  It is a short-hand for writing "[^\n]", and
668           is identical to the "." metasymbol, except under the "/s" flag,
669           which changes the meaning of ".", but not "\N".
670
671           Note that "\N{...}" can mean a named or numbered character .
672
673           Mnemonic: Complement of \n.
674
675       \R  "\R" matches a generic newline; that is, anything considered a
676           linebreak sequence by Unicode. This includes all characters matched
677           by "\v" (vertical whitespace), and the multi character sequence
678           "\x0D\x0A" (carriage return followed by a line feed, sometimes
679           called the network newline; it's the end of line sequence used in
680           Microsoft text files opened in binary mode). "\R" is equivalent to
681           "(?>\x0D\x0A|\v)".  (The reason it doesn't backtrack is that the
682           sequence is considered inseparable.  That means that
683
684            "\x0D\x0A" =~ /^\R\x0A$/   # No match
685
686           fails, because the "\R" matches the entire string, and won't
687           backtrack to match just the "\x0D".)  Since "\R" can match a
688           sequence of more than one character, it cannot be put inside a
689           bracketed character class; "/[\R]/" is an error; use "\v" instead.
690           "\R" was introduced in perl 5.10.0.
691
692           Note that this does not respect any locale that might be in effect;
693           it matches according to the platform's native character set.
694
695           Mnemonic: none really. "\R" was picked because PCRE already uses
696           "\R", and more importantly because Unicode recommends such a
697           regular expression metacharacter, and suggests "\R" as its
698           notation.
699
700       \X  This matches a Unicode extended grapheme cluster.
701
702           "\X" matches quite well what normal (non-Unicode-programmer) usage
703           would consider a single character.  As an example, consider a G
704           with some sort of diacritic mark, such as an arrow.  There is no
705           such single character in Unicode, but one can be composed by using
706           a G followed by a Unicode "COMBINING UPWARDS ARROW BELOW", and
707           would be displayed by Unicode-aware software as if it were a single
708           character.
709
710           The match is greedy and non-backtracking, so that the cluster is
711           never broken up into smaller components.
712
713           See also "\b{gcb}".
714
715           Mnemonic: eXtended Unicode character.
716
717       Examples
718
719        $str =~ s/foo\Kbar/baz/g; # Change any 'bar' following a 'foo' to 'baz'
720        $str =~ s/(.)\K\g1//g;    # Delete duplicated characters.
721
722        "\n"   =~ /^\R$/;         # Match, \n   is a generic newline.
723        "\r"   =~ /^\R$/;         # Match, \r   is a generic newline.
724        "\r\n" =~ /^\R$/;         # Match, \r\n is a generic newline.
725
726        "P\x{307}" =~ /^\X$/     # \X matches a P with a dot above.
727
728
729
730perl v5.28.2                      2018-11-01                PERLREBACKSLASH(1)
Impressum