Skip to content

Commit

Permalink
Some additional amendments to updated Literals section
Browse files Browse the repository at this point in the history
  • Loading branch information
threadless-screw committed Aug 12, 2019
1 parent 7b5d7d3 commit b1df019
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions doc/Language/regexes.pod6
Expand Up @@ -66,29 +66,29 @@ and L<multi line/embedded comments|
A regex describes a pattern to be matched in terms of literals and
metacharacters. Alphanumeric characters and the underscore C<_> constitute the
literals: these characters match themselves and nothing else. Other characters
are metacharacters and may, as such, have a special meaning, either alone (such
as the dot C<.>, which acts as a wildcard) or together with other characters in
larger metasyntactic constructs (such as C«<?before ...>», which defines a
lookahead assertion). But before looking at the metacharacters and their
particular uses, let's explore the relation between literals and metacharacters
in some more detail.
act as metacharacters and may, as such, have a special meaning, either by
themselves (such as the dot C<.>, which serves as a wildcard) or together with
other characters in larger metasyntactic constructs (such as C«<?before ...>»,
which defines a lookahead assertion). But before looking at metacharacters and
their particular uses, let's first explore the relation between literals and
metacharacters in some more detail.
In its simplest form a regex comprises only literals:
if 'properly' ~~ / perl / {
say "'properly' contains 'perl'"; # OUTPUT: «'properly' contains 'perl'␤»
}
If you want a regex to match one or more metacharacters literally, the
metacharacters must either be escaped using a backslash, or be quoted using
single or double quotes.
If you want a regex to literally match one or more characters that normally act
as metacharacters, these characters must either be escaped using a backslash, or
be quoted using single or double quotes.
The backslash serves as a switch. It switches a single metacharacter into a
literal, and vice versa:
/ \# / # matches the hash metacharacter literally
/ \w / # turns literal 'w' into a character class (see below)
/Hallelujah\!/ # matches string 'Halleluja!' incl. exclamation mark
/Hallelujah\!/ # matches string 'Hallelujah!' incl. exclamation mark
Even if a metacharacter does not (yet) have a special meaning in Perl 6,
escaping (or quoting) it is required to ensure that the regex compiles and
Expand All @@ -104,17 +104,22 @@ single I<and multiple> metacharacters may be turned into literally matching
strings by quoting them using single or double quotes:
/ "abc" / # you may quote literals like this, but it has no effect
/ "two words" / # quoting a space renders it significant
/ "Hallelujah!" / # yet, this form is generally preferred over /Hallelujah\!/
/ "two words" / # quoting a space renders it significant, so this matches
# the string 'two words' including the intermediate space
/ '#!:@' / # this regex matches the string of metacharacters '#!:@'
Quoting does not turn any metacharacter into a literal, however. This is due to
the fact that quotes allow for backslash-escapes and interpolation.
Quoting does not turn every metacharacter into a literal, however. This is due
to the fact that quotes allow for backslash-escapes and interpolation.
Specifically: in single quotes, the backslash may be used to escape single
quotes and the backslash itself; double quotes additionally enable the
interpolation of variables, and of code blocks of the form C<{...}>:
/ '\\\'' / # matches a backslash followed by a single quote: \'
/ '\' / # !! error: this is NOT the way to literally match a
# backslash because now it escapes the second quote
my $x = 'Hi';
/ "$x there!" / # matches the string 'Hi there!'
Expand Down

0 comments on commit b1df019

Please sign in to comment.