Skip to content

Commit

Permalink
Adds info on :my; refs #1475
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ committed May 21, 2018
1 parent ad5ae59 commit ef6e19e
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions doc/Language/regexes.pod6
Expand Up @@ -1022,8 +1022,16 @@ it in a variable first:
# CORRECT: $0 is saved into a variable outside the second capture
# before it is used inside (the `{}` is needed to update the current match)
say "11" ~~ /(\d) {} :my $c = $0; ($c)/;
# OUTPUT: «「11」␤ 0 => 「1」␤ 1 => 「1」␤»
say "11" ~~ /(\d) {} :my $c = $0; ($c)/; # OUTPUT: «「11」␤ 0 => 「1」␤ 1 => 「1」␤»
say "Matched $c"; # OUTPUT: «␤Matched 1␤»
C<:my> helps scoping the C<$c> variable within the regex and beyond; in this case we can use it in the next sentence to show what has been matched inside the regex. This can be used for debugging inside regular expressions, for instance:
my $paragraph="line\nline2\nline3";
$paragraph ~~ rx| :my $counter = 0; ( \V* { ++$counter } ) *%% \n |;
say "Matched $counter lines"; # OUTPUT: «Matched 3 lines␤»
=head2 X<Named captures|regex, Named captures>
Expand Down

0 comments on commit ef6e19e

Please sign in to comment.