@@ -86,44 +86,49 @@ be quoted using single or double quotes.
86
86
The backslash serves as a switch. It switches a single metacharacter into a
87
87
literal, and vice versa:
88
88
89
- / \# / # matches the hash metacharacter literally
90
- / \w / # turns literal 'w' into a character class (see below)
91
- /Hallelujah\!/ # matches string 'Hallelujah!' incl. exclamation mark
89
+ / \# /; # matches the hash metacharacter literally
90
+ / \w /; # turns literal 'w' into a character class (see below)
91
+ /Hallelujah\!/; # matches string 'Hallelujah!' incl. exclamation mark
92
92
93
93
Even if a metacharacter does not (yet) have a special meaning in Perl 6,
94
94
escaping (or quoting) it is required to ensure that the regex compiles and
95
95
matches the character literally. This allows the clear distinction between
96
96
literals and metacharacters to be maintained:
97
97
98
- / \, / # matches a literal comma ','
99
- / , / # !! error: a yet meaningless/unrecognized metacharacter
98
+ / \, /; # matches a literal comma ','
99
+
100
+ = for code :skip-test<deliberate error>
101
+ / , /; # !! error: a yet meaningless/unrecognized metacharacter
100
102
# does not automatically match literally
101
103
102
104
While an escaping backslash exerts its effect on the next individual character,
103
105
single I < and multiple > metacharacters may be turned into literally matching
104
106
strings by quoting them using single or double quotes:
105
107
106
- / "abc" / # you may quote literals like this, but it has no effect
107
- / "Hallelujah!" / # yet, this form is generally preferred over /Hallelujah\!/
108
+ / "abc" /; # you may quote literals like this, but it has no effect
109
+ / "Hallelujah!" /; # yet, this form is generally preferred over /Hallelujah\!/
108
110
109
- / "two words" / # quoting a space renders it significant, so this matches
111
+ / "two words" /; # quoting a space renders it significant, so this matches
110
112
# the string 'two words' including the intermediate space
111
113
112
- / '#!:@' / # this regex matches the string of metacharacters '#!:@'
114
+ / '#!:@' /; # this regex matches the string of metacharacters '#!:@'
113
115
114
116
Quoting does not turn every metacharacter into a literal, however. This is due
115
117
to the fact that quotes allow for backslash-escapes and interpolation.
116
118
Specifically: in single quotes, the backslash may be used to escape single
117
119
quotes and the backslash itself; double quotes additionally enable the
118
120
interpolation of variables, and of code blocks of the form C < {...} > :
119
121
120
- / '\\\'' / # matches a backslash followed by a single quote: \'
121
- / '\' / # !! error: this is NOT the way to literally match a
122
+ / '\\\'' /; # matches a backslash followed by a single quote: \'
123
+
124
+ = for code :skip-test<deliberate error>
125
+ / '\' /; # !! error: this is NOT the way to literally match a
122
126
# backslash because now it escapes the second quote
127
+
123
128
my $x = 'Hi';
124
- / "$x there!" / # matches the string 'Hi there!'
129
+ / "$x there!" /; # matches the string 'Hi there!'
125
130
126
- / "1 + 1 = {1+1}" / # matches the string '1 + 1 = 2'
131
+ / "1 + 1 = {1+1}" /; # matches the string '1 + 1 = 2'
127
132
128
133
Strings are searched left to right, so it is enough if only part of the string
129
134
matches the regex:
0 commit comments