How can the Raku behavior on capturing group in alternate be the same as Perl

Viewed 115

How can Raku behavior on capturing group in alternate be just like Perl regex' one e.g.

> 'abefo' ~~ /a [(b) | (c) (d)] (e)[(f)|(g)]/
「abef」
 0 => 「b」
 2 => 「e」
 3 => 「f」

needed to be 'usual' Perl regex result (let index system stay Raku):

 $0 = 'b'
 $1 = undef
 $2 = undef 
 $3 = e
 $4 = f

Please give useful guide.

2 Answers

Quoting the Synopsis 5: Regexes and Rules design speculation document:

it is still possible to mimic the monotonic Perl 5 capture indexing semantics

Inserting a $3= for the (e):

/ a [ (b) | (c) (d) ] $3=(e) [ (f) | (g) ] /

andthen say 'abefo' ~~ $_

「abef」
 0 => 「b」
 3 => 「e」
 4 => 「f」

I've briefly looked for a mention of this in the doc but didn't see it.

So maybe we should file doc issues for mentioning this, presumably in Capture numbers and $ ($1, $2, ...).

Unclear as to the question, but surely going back to Perl5 semantics vs Raku means changing the alternation operator.

Perl5's | alternation operator is one in which the "first matching alternative" wins. The equivalent alternation operator in Raku is ||.

Raku's | alternation operator performs Longest Token Matching (LTM), (which roughly means if you separate your alternatives by |, you can spend less time ordering them by longest token to get the desired result).

https://docs.raku.org/language/regexes#Alternation:_||
https://docs.raku.org/language/regexes#Longest_alternation:_|

(As for capture numbering, maybe you can submit a request that that gets handled by the :Perl5 or :P5 regex adverb? See: https://docs.raku.org/language/regexes#Perl_compatibility_adverb )

Related