develooper Front page | perl.perl6.users | Postings from September 2022

Regex surprises

Thread Previous | Thread Next
From:
Sean McAfee
Date:
September 12, 2022 20:06
Subject:
Regex surprises
Message ID:
CANan03byU6XsGbvjeE+aP415BPPvinTY2Ua5paHpBuyv641Pnw@mail.gmail.com
Hello--

I stumbled across a couple of bits of surprising regex behavior today.

First, consider:

S:g[ x { say 1 } ] = say 2 given "xx"

I expected this to print 1, 2, 1, 2, but it prints 1, 1, 2, 2.  So it looks
like, in a global substitution like this, Raku doesn't look for successive
matches and then evaluate the replacements as it goes, but finds all of the
matches *first* and then works through the substitutions.  In my actual
problem I was mutating state in the regex code block, and then it didn't
work because all of the mutations happened before even a single replacement
was evaluated.  Is it really meant to work this way?

Next, consider:

> "y" ~~ /(x)|(y)/
「y」
 0 => 「y」

y is in the second set of grouping parentheses, so I expected it to be in
group 1, but it's in group 0.  So it looks like the group index starts from
0 in every branch of an alternation.  I do so much regex slinging I'm
amazed it took me so long to discover this, if it's not a relatively recent
change.  I'm accustomed to being able to determine which alternation branch
was matched by checking which group is defined (in other languages too, not
just Raku).  This kind of thing:

S:g[(x)|(y)] = $0 ?? x-replacement !! y-replacement

I guess instead I need to do this:

S:g[x|y] = $/ eq 'x' ?? x-replacement !! y-replacement

It seems very strange that I need to re-examine the match to know what
matched.  The match should be able to tell me what matched.  Or is there
perhaps some alternate way for me to tell which alternative matched?

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About