ertical.
=head3 Unicode classes
C<\pP> (where C is a single letter) and C<\p{Property}> are used to
match a character that matches the given Unicode property; properties
include things like "letter", or "thai character". Capitalizing the
sequence to C<\PP> and C<\P{Property}> make the sequence match a character
that doesn't match the given Unicode property. For more details, see
L and
L.
Mnemonic: Iroperty.
=head2 Referencing
If capturing parenthesis are used in a regular expression, we can refer
to the part of the source string that was matched, and match exactly the
same thing. There are three ways of referring to such I:
absolutely, relatively, and by name.
=for later add link to perlrecapture
=head3 Absolute referencing
A backslash sequence that starts with a backslash and is followed by a
number is an absolute reference (but be aware of the caveat mentioned above).
If the number is I, it refers to the Nth set of parenthesis - whatever
has been matched by that set of parenthesis has to be matched by the C<\N>
as well.
=head4 Examples
/(\w+) \1/; # Finds a duplicated word, (e.g. "cat cat").
/(.)(.)\2\1/; # Match a four letter palindrome (e.g. "ABBA").
=head3 Relative referencing
New in perl 5.10.0 is a different way of referring to capture buffers: C<\g>.
C<\g> takes a number as argument, with the number in curly braces (the
braces are optional). If the number (N) does not have a sign, it's a reference
to the Nth capture group (so C<\g{2}> is equivalent to C<\2> - except that
C<\g> always refers to a capture group and will never be seen as an octal
escape). If the number is negative, the reference is relative, referring to
the Nth group before the C<\g{-N}>.
The big advantage of C<\g{-N}> is that it makes it much easier to write
patterns with references that can be interpolated in larger patterns,
even if the larger pattern also contains capture groups.
Mnemonic: Iroup.
=head4 Examples
/(A) # Buffer 1
( # Buffer 2
(B) # Buffer 3
\g{-1} # Refers to buffer 3 (B)
\g{-3} # Refers to buffer 1 (A)
)
/x; # Matches "ABBA".
my $qr = qr /(.)(.)\g{-2}\g{-1}/; # Matches 'abab', 'cdcd', etc.
/$qr$qr/ # Matches 'ababcdcd'.
=head3 Named referencing
Also new in perl 5.10.0 is the use of named capture buffers, which can be
referred to by name. This is done with C<\g{name}>, which is a
backreference to the capture buffer with the name I.
To be compatible with .Net regular expressions, C<\g{name}> may also be
written as C<\k{name}>, C<< \k >> or C<\k'name'>.
Note that C<\g{}> has the potential to be ambiguous, as it could be a named
reference, or an absolute or relative reference (if its argument is numeric).
However, names are not allowed to start with digits, nor are allowed to
contain a hyphen, so there is no ambiguity.
=head4 Examples
/(?\w+) \g{word}/ # Finds duplicated word, (e.g. "cat cat")
/(?\w+) \k{word}/ # Same.
/(?\w+) \k/ # Same.
/(?.)(?.)\g{letter2}\g{letter1}/
# Match a four letter palindrome (e.g. "ABBA")
=head2 Assertions
Assertions are conditions that have to be true -- they don't actually
match parts of the substring. There are six assertions that are written as
backslash sequences.
=over 4
=item \A
C<\A> only matches at the beginning of the string. If the C modifier
isn't used, then C\A/> is equivalent with C^/>. However, if the C
modifier is used, then C^/> matches internal newlines, but the meaning
of C\A/> isn't changed by the C modifier. C<\A> matches at the beginning
of the string regardless whether the C modifier is used.
=item \z, \Z
C<\z> and C<\Z> match at the end of the string. If the C modifier isn't
used, then C\Z/> is equivalent with C$/>, that is, it matches at the
end of the string, or before the newline at the end of the string. If the
C modifier is used, then C$/> matches at internal newlines, but the
meaning of C\Z/> isn't changed by the C modifier. C<\Z> matches at
the end of the string (or just before a trailing newline) regardless whether
the C modifier is used.
C<\z> is just like C<\Z>, except that it will not match before a trailing
newline. C<\z> will only match at the end of the string - regardless of the
modifiers used, and not before a newline.
=item \G
C<\G> is usually only used in combination with the C modifier. If the
C modifier is used (and the match is done in scalar context), Perl will
remember where in the source string the last match ended, and the next time,
it will start the match from where it ended the previous time.
C<\G> matches the point where the previous match ended, or the beginning
of the string if there was no previous match.
=for later add link to perlremodifiers
Mnemonic: Ilobal.
=item \b, \B
C<\b> matches at any place between a word and a non-word character; C<\B>
matches at any place between characters where C<\b> doesn't match. C<\b>
and C<\B> assume there's a non-word character before the beginning and after
the end of the source string; so C<\b> will match at the beginning (or end)
of the source string if the source string begins (or ends) with a word
character. Otherwise, C<\B> will match.
Mnemonic: Ioundary.
=back
=head4 Examples
"cat" =~ /\Acat/; # Match.
"cat" =~ /cat\Z/; # Match.
"cat\n" =~ /cat\Z/; # Match.
"cat\n" =~ /cat\z/; # No match.
"cat" =~ /\bcat\b/; # Matches.
"cats" =~ /\bcat\b/; # No match.
"cat" =~ /\bcat\B/; # No match.
"cats" =~ /\bcat\B/; # Match.
while ("cat dog" =~ /(\w+)/g) {
print $1; # Prints 'catdog'
}
while ("cat dog" =~ /\G(\w+)/g) {
print $1; # Prints 'cat'
}
=head2 Misc
Here we document the backslash sequences that don't fall in one of the
categories above. They are:
=over 4
=item \C
C<\C> always matches a single octet, even if the source string is encoded
in UTF-8 format, and the character to be matched is a multi-octet character.
C<\C> was introduced in perl 5.6.
Mnemonic: oItet.
=item \K
This is new in perl 5.10.0. Anything that is matched left of C<\K> is
not included in C<$&> - and will not be replaced if the pattern is
used in a substitution. This will allow you to write C
instead of C or C.
Mnemonic: Ieep.
=item \R
C<\R> matches a I, that is, anything that is considered
a newline by Unicode. This includes all characters matched by C<\v>
(vertical white space), and the multi character sequence C<"\x0D\x0A">
(carriage return followed by a line feed, aka the network newline, or
the newline used in Windows text files). C<\R> is equivalent with
C<< (?>\x0D\x0A)|\v) >>. Since C<\R> can match a more than one character,
it cannot be put inside a bracketed character class; C[\R]/> is an error.
C<\R> was introduced in perl 5.10.0.
Mnemonic: none really. C<\R> was picked because PCRE already uses C<\R>,
and more importantly because Unicode recommends such a regular expression
metacharacter, and suggests C<\R> as the notation.
=item \X
This matches an extended Unicode I, and
is equivalent to C<< (?>\PM\pM*) >>. C<\PM> matches any character that is
not considered a Unicode mark character, while C<\pM> matches any character
that is considered a Unicode mark character; so C<\X> matches any non
mark character followed by zero or more mark characters. Mark characters
include (but are not restricted to) I and
I.
C<\X> matches quite well what normal (non-Unicode-programmer) usage
would consider a single character: for example a base character
(the C<\PM> above), for example a letter, followed by zero or more
diacritics, which are I (the C<\pM*> above).
Mnemonic: eItended Unicode character.
=back
=head4 Examples
"\x{256}" =~ /^\C\C$/; # Match as chr (256) takes 2 octets in UTF-8.
$str =~ s/foo\Kbar/baz/g; # Change any 'bar' following a 'foo' to 'baz'.
$str =~ s/(.)\K\1//g; # Delete duplicated characters.
"\n" =~ /^\R$/; # Match, \n is a generic newline.
"\r" =~ /^\R$/; # Match, \r is a generic newline.
"\r\n" =~ /^\R$/; # Match, \r\n is a generic newline.
"P\x{0307}" =~ /^\X$/ # \X matches a P with a dot above.
=cut