Skip to content

Commit 2df45c6

Browse files
committed
A complete example of <?{}>.
See issue 1757
1 parent 3f1dfd0 commit 2df45c6

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

doc/Language/regexes.pod6

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,9 @@ a semantic evaluation of the match before the regular expression continues. In o
13241324
to check in aboolean context a part of a regular expression and therefore invalidate the whole match (or allow it
13251325
to continue) even if the match succeed from a syntactic point of view.
13261326
1327+
In particular the C«<?{}>» operator requires a C<True> value in order to allow the regular expression to match, while its
1328+
negate form C«<!{}>» requires a C<False> value.
1329+
13271330
In order to demonstrate the above operator, please consider the following example that involves
13281331
a simple IPv4 address matching:
13291332
@@ -1347,6 +1350,16 @@ $localhost ~~ / ^ <ipv4-octet> ** 4 % "." $ /;
13471350
say $/<ipv4-octet>; # OUTPUT: Nil
13481351
=end code
13491352
1353+
From the above examples, it should be clear that it is possible to improve the semantic check, for instance
1354+
ensuring that each I<octet> is really a valid IPv4 octect:
1355+
1356+
=begin code
1357+
my $localhost = '127.0.0.1';
1358+
my regex ipv4-octet { \d ** 1..3 <?{ $/.Int <= 255 && $/.Int >= 0 }> }
1359+
$localhost ~~ / ^ <ipv4-octet> ** 4 % "." $ /;
1360+
say $/<ipv4-octet>; # OUTPUT: [「127」 「0」 「0」 「1」]
1361+
=end code
1362+
13501363
13511364
=head1 Adverbs
13521365

0 commit comments

Comments
 (0)