@@ -69,15 +69,13 @@ literals: these characters match themselves and nothing else. Other characters
69
69
act as metacharacters and may, as such, have a special meaning, either by
70
70
themselves (such as the dot C < . > , which serves as a wildcard) or together with
71
71
other characters in larger metasyntactic constructs (such as C « <?before ...> » ,
72
- which defines a lookahead assertion). But before looking at metacharacters and
73
- their particular uses, let's first explore the relation between literals and
74
- metacharacters in some more detail.
72
+ which defines a lookahead assertion).
75
73
76
74
In its simplest form a regex comprises only literals:
77
75
78
- if 'properly' ~~ / perl / {
79
- say "'properly' contains 'perl'"; # OUTPUT: «'properly' contains 'perl'»
80
- }
76
+ /Cześć/; # "Hello" in Polish
77
+ /こんばんは/; # "Good afternoon" in Japanese
78
+ /Καλησπέρα/; # "Good evening" in Greek
81
79
82
80
If you want a regex to literally match one or more characters that normally act
83
81
as metacharacters, these characters must either be escaped using a backslash, or
@@ -93,43 +91,50 @@ literal, and vice versa:
93
91
Even if a metacharacter does not (yet) have a special meaning in Perl 6,
94
92
escaping (or quoting) it is required to ensure that the regex compiles and
95
93
matches the character literally. This allows the clear distinction between
96
- literals and metacharacters to be maintained:
94
+ literals and metacharacters to be maintained. So, for instance, to match a
95
+ comma this will work:
97
96
98
97
/ \, /; # matches a literal comma ','
99
98
99
+ while this will fail:
100
100
= for code :skip-test<deliberate error>
101
- / , /; # !! error: a yet meaningless/unrecognized metacharacter
102
- # does not automatically match literally
101
+ / , /; # !! error: a yet meaningless/unrecognized metacharacter
102
+ # does not automatically match literally
103
103
104
104
While an escaping backslash exerts its effect on the next individual character,
105
- single I < and multiple > metacharacters may be turned into literally matching
106
- strings by quoting them using single or double quotes:
105
+ both a single metacharacter and a sequence of metacharacters may be turned into
106
+ literally matching strings by quoting them in single or double quotes:
107
107
108
- / "abc" /; # you may quote literals like this, but it has no effect
108
+ / "abc" /; # quoting literals does not make them more literal
109
109
/ "Hallelujah!" /; # yet, this form is generally preferred over /Hallelujah\!/
110
110
111
111
/ "two words" /; # quoting a space renders it significant, so this matches
112
112
# the string 'two words' including the intermediate space
113
113
114
114
/ '#!:@' /; # this regex matches the string of metacharacters '#!:@'
115
115
116
- Quoting does not turn every metacharacter into a literal, however. This is due
117
- to the fact that quotes allow for backslash-escapes and interpolation.
118
- Specifically: in single quotes, the backslash may be used to escape single
119
- quotes and the backslash itself; double quotes additionally enable the
120
- interpolation of variables, and of code blocks of the form C < {...} > :
116
+ Quoting does not simply turn every metacharacter into a literal, however. This
117
+ is because quotes allow for backslash-escapes and interpolation. Specifically:
118
+ in single quotes, the backslash may be used to escape single quotes and the
119
+ backslash itself; double quotes additionally enable the interpolation of
120
+ variables, and of code blocks of the form C < {...} > . Hence all of this works :
121
121
122
122
/ '\\\'' /; # matches a backslash followed by a single quote: \'
123
123
124
- = for code :skip-test<deliberate error>
125
- / '\' /; # !! error: this is NOT the way to literally match a
126
- # backslash because now it escapes the second quote
127
-
128
124
my $x = 'Hi';
129
125
/ "$x there!" /; # matches the string 'Hi there!'
130
126
131
127
/ "1 + 1 = {1+1}" /; # matches the string '1 + 1 = 2'
132
128
129
+ while these examples illustrate mistakes that you will want to avoid:
130
+ = begin code :skip-test<deliberate error>
131
+ / '\' /; # !! error: this is NOT the way to literally match a
132
+ # backslash because now it escapes the second quote
133
+
134
+ /"Price tag $0.50"/; # !! error: "$0" is interpreted as the first positional
135
+ # capture (which is Nil), not as '$0'
136
+ = end code
137
+
133
138
Strings are searched left to right, so it is enough if only part of the string
134
139
matches the regex:
135
140
0 commit comments