rsFalse has asked for the wisdom of the Perl Monks concerning the following question:
Or you can see it with some tests here on a platform - http://codeforces.com/contest/633/submission/16382698chomp( (undef, $_, undef, @words) = <> ); %restore_case = map { lc, $_ } @words; $list = join '|', map { scalar reverse lc } @words; m/^ (?: ($list | $ | (?{ pop @submatches }) (?!) ) (?{ push @submatches, $^N }) )+ $/x; pop @submatches; print join ' ', map { $restore_case{ scalar reverse } } @submatches
I wanted to discuss: are there other solutions using regexes, and is it possible to increase performance?m/^ (?: ( $list # dictionary joined with '|' | $ # if end of line then success, so don't pop array | (?{ pop @submatches }) (?!) # pop array and suddenly fa +il ) (?{ push @submatches, $^N }) # after every matched word, push + it to array # $^N - last captured group )+ $/x;
And rewrote it in push/pop array manner:":aa2bb4cc6dd8" =~ / (:) (?{ [] }) # initialize $^R (?: (\w\w) (\d) # add captures to $^R: (?{ [@{$^R}, [$2, $3]] }) )* /x;
And it seems to work much more faster on bigger inputs. I was measuring on ideone.com (semicolon removed):my @R; ":aa2bb4cc6dd8" =~ / (:) (?: (\w\w) (\d) (?{ push @R, [$2, $3] }) )* /x;
And this code worked 0.06 s (string x 5e3) (http://ideone.com/n3cKVr):use Data::Dumper; sub dd { print Dumper(shift) }; ("aa2bb4cc6dd8" x 1e3) =~ / (?{ [] }) # initialize $^R (?: (\w\w) (\d) # add captures to $^R: (?{ [@{$^R}, [$1, $2]] }) )* /x; dd @{ $^R }[ -1 ]
both output:use Data::Dumper; sub dd { print Dumper(shift) }; my @R = (); ("aa2bb4cc6dd8" x 5e3) =~ / (?: (\w\w) (\d) # push captures to @R: (?{ push @R, [$1, $2] }) )* /x; dd @R[ -1 ]
$VAR1 = [ 'dd', '8' ];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Multi_captures needed
by Eily (Monsignor) on Feb 28, 2016 at 12:38 UTC | |
|
Re: Multi_captures needed
by AnomalousMonk (Archbishop) on Feb 28, 2016 at 19:05 UTC |