Skip to content

Commit 94bdf62

Browse files
authored
Update regexes.pod6
1 parent 082d518 commit 94bdf62

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

doc/Language/regexes.pod6

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,44 +1257,34 @@ documents.
12571257
12581258
=head1 X<Regex Interpolation|regex, Regex Interpolation>
12591259
1260-
If you want to build a regex using pattern given at the runtime, regex
1261-
interpolation is what you looking for.
1260+
If you want to build a regex using a pattern given at runtime, regex
1261+
interpolation is what you are looking for.
12621262
1263-
There are two ways you can interpolate a string into regex as a pattern,
1264-
which are quite similar to interpolate it into a double-quote string.
1265-
That is using C«<$pattern>» or C«<{$pattern.method}>».
1263+
There are four ways you can interpolate a string into regex as a pattern.
1264+
That is using C«$pattern», C«$($pattern)», C«<$pattern>» or
1265+
C«<{$pattern.method}>».
12661266
12671267
my $text = 'camelia';
1268-
my $pattern = '\w+';
1269-
my $pattern_ = 'ailemac';
1270-
say $text ~~ / <$pattern> /; # OUTPUT: «「camelia」␤»
1271-
say $text ~~ / <{$pattern}> /; # OUTPUT: «「camelia」␤»
1272-
say $text ~~ / <{$pattern_.flip}> /; # OUTPUT: «「camelia」␤»
1273-
# say $text ~~ / <$pattern_.flip> /; !!Compile Error!!
1268+
my $pattern0 = 'camelia';
1269+
my $pattern1 = 'ailemac';
1270+
my $pattern2 = '\w+';
1271+
1272+
say $text ~~ / $pattern0 /; # OUTPUT: «「camelia」␤»
1273+
say $text ~~ / $($pattern0) /; # OUTPUT: «「camelia」␤»
1274+
say $text ~~ / $($pattern1.flip) /; # OUTPUT: «「camelia」␤»
1275+
say 'ailemacxflip' ~~ / $pattern1.flip /; # OUTPUT: «「ailemacxflip」␤»
1276+
say '\w+' ~~ / $pattern2 /; # OUTPUT: «「\w+」␤»
1277+
say '\w+' ~~ / $($pattern2) /; # OUTPUT: «「\w+」␤»
1278+
1279+
say $text ~~ / <{$pattern1.flip}> /; # OUTPUT: «「camelia」␤»
1280+
# say $text ~~ / <$pattern1.flip> /; # !!Compile Error!!
1281+
say $text ~~ / <$pattern2> /; # OUTPUT: «「camelia」␤»
1282+
say $text ~~ / <{$pattern2}> /; # OUTPUT: «「camelia」␤»
1283+
1284+
Note that the first two syntax interpolate the string lexically, while
1285+
C«<$pattern>» and C«<{$pattern.method}>» causes L«implicit EVAL|
1286+
/language/traps#<{$x}>_vs_$($x):_Implicit_EVAL», which is a known trap.
12741287
1275-
Note that this syntax causes L«implicit EVAL|
1276-
/language/traps#<{$x}>_vs_$($x):_Implicit_EVAL», that is a known trap.
1277-
1278-
When your pattern is a lexical string, there are two more syntax helping you
1279-
interpolate it.
1280-
1281-
my $text = 'camelia';
1282-
my $string = 'camelia';
1283-
my $string_ = 'ailemac';
1284-
say $text ~~ / $string /; # OUTPUT: «「camelia」␤»
1285-
say $text ~~ / $($string) /; # OUTPUT: «「camelia」␤»
1286-
say $text ~~ / $($string_.flip) /; # OUTPUT: «「camelia」␤»
1287-
say $text ~~ / $string_.flip /; # OUTPUT: «Nil␤»
1288-
say 'ailemacxflip' ~~ / $string_.flip /; # OUTPUT: «「ailemacxflip」␤»
1289-
1290-
Because it interpolates a string lexically, it doesn't EVAL the string
1291-
implicitly.
1292-
1293-
my $text = '42';
1294-
my $text_ = '\d+';
1295-
my $lexical = '\d+';
1296-
say $text ~~ / $($lexical) /; # OUTPUT: «Nil␤»
1297-
say $text_ ~~ / $($lexical) /; # OUTPUT: «「\d+」␤»
12981288
12991289
=head1 Adverbs
13001290

0 commit comments

Comments
 (0)