Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backreferences in regexes match, but do not capture

Tags:

regex

raku

This program

say "zipi zape" ~~ /(\w)\w» \s+ \w+({$0})/;

returns

「pi zape」
 0 => 「p」
 1 => 「」

which I interpret as the backreference to the first match being matched to a zero-width match? Maybe because it's matched to $0, which is itemized to '' outside the regex? How could I use these backreferences, and capture at the same time the match? Note: this related to this documentation issue, which requires clarification of the use of backreferences.

like image 616
jjmerelo Avatar asked Mar 04 '23 18:03

jjmerelo


1 Answers

According to the documentation:

If you need to refer to a capture from within another capture, store it in a variable first

So you could use:

say "zipi zape" ~~ /(\w){} :my $c = $0; \w » \s+ \w+($c)/;

Output:

「pi zap」
 0 => 「p」
 1 => 「p」
like image 108
Håkon Hægland Avatar answered Mar 13 '23 02:03

Håkon Hægland