in reply to while-loop regex substitution with 'g' option

"g" always means "all the matches".

With s///g, it performs all the substitutions.

With m//g, it normally returns all the matches.

This is very similar.

I said "normally" because m//g in scalar context can't return all the matches without external help. That's why a loop needs to be introduced.

You're looking for

$x =~ s{ \s* (foo\w) \s* }{ push @captured, $1; "" }xeg;

Replies are listed 'Best First'.
Re^2: while-loop regex substitution with 'g' option
by ibm1620 (Hermit) on Apr 22, 2024 at 04:43 UTC
    Beautiful. Thanks.