in reply to Extracting /regex/mg in a while loop

Your Method #3 doesn't work because a m//g regex with capture groups in list context returns a list of the substrings matched by any capturing parentheses in the regular expression, that is, ($1, $2...), repeated for each match (see my node here).

my @output = $x=~/(^A|^B)(.+)$/mg; print Dumper(\@output); # $VAR1 = ['A', ' data for A', 'B', ' data for b', 'A', ' data for A', + ...

I would guess that in Method #2, the overhead of splitting the string into lines and then running a regex match on each of the lines (as opposed to a single regex iterating through a single string) might be what is causing the slowdown.

But I would address your comment in Method #1 about using names as follows, and this is the variation I might suggest in this case: named capture groups (see also perlre).

while ( $x =~ m{^ (?<name>A|B) (?<data>.+) $}xmg ) { print "$+{name} method1 $+{data}\n" }

(There are of course plenty of other ways, such as m/\G.../gc parsing.)