Hello,

I want solve this problem using regexes:
Given a "sorted" target string and a sequence of sizes of groups of equal consecutive characters, find and output characters from a string which corresponds to each group.

I wrote a code, which glues smaller regexes to a bigger one, and uses re-eval ((?{ ... })). I use a "stack" array for saving captured characters, and use push/pop to manipulate it.
Also I used $^N variable interpolating it inside double-quotes. This fails to run without "use re 'eval'". How can I overcome it? Any alternatives to $^N for access a last captured group outside of regex?
Any ideas of alternative solutions for a given problem? Code:
#!/usr/bin/perl use warnings; use strict; use re 'eval'; $\ = $/; while(<DATA>){ print '-' x 15; chomp; my $target = $_; my @groups = split ' ', <DATA>; print "target:[$target]"; print "groups:[@groups]"; my @chars_seq; my $re = join " (?!\\g{-1}).*? \n", map { sprintf "(?: " . "( (.)\\g{-1}{%d} ) " . "(?{ push \@chars_seq, \$\^N; }) " . "(?: (?=) | (?{ pop \@chars_seq }) (*FAIL) ) " . "(?{ print join ' ', \@chars_seq; })" . ")" , $_ - 1 } @groups; print for "regex:[", $re, "]"; $re =~ s/\n//g; $target =~ / $re /x or print "FAIL!"; print "character sequence:[@chars_seq]"; } __DATA__ cbaa 1 2 cba 1 bbaa 1 2 cccqrrtaaa 2 2 2 cccqrrtaaa 1 1 2 2 cccqrrtaaa 1 1 1 1 3
OUTPUT:
--------------- target:[cbaa] groups:[1 2] regex:[ (?: ( (.)\g{-1}{0} ) (?{ push @chars_seq, $^N; }) (?: (?=) | (?{ pop @ +chars_seq }) (*FAIL) ) (?{ print join ' ', @chars_seq; })) (?!\g{-1}) +.*? (?: ( (.)\g{-1}{1} ) (?{ push @chars_seq, $^N; }) (?: (?=) | (?{ pop @ +chars_seq }) (*FAIL) ) (?{ print join ' ', @chars_seq; })) ] c c aa character sequence:[c aa] --------------- target:[cba] groups:[1] regex:[ (?: ( (.)\g{-1}{0} ) (?{ push @chars_seq, $^N; }) (?: (?=) | (?{ pop @ +chars_seq }) (*FAIL) ) (?{ print join ' ', @chars_seq; })) ] c character sequence:[c] --------------- target:[bbaa] groups:[1 2] regex:[ (?: ( (.)\g{-1}{0} ) (?{ push @chars_seq, $^N; }) (?: (?=) | (?{ pop @ +chars_seq }) (*FAIL) ) (?{ print join ' ', @chars_seq; })) (?!\g{-1}) +.*? (?: ( (.)\g{-1}{1} ) (?{ push @chars_seq, $^N; }) (?: (?=) | (?{ pop @ +chars_seq }) (*FAIL) ) (?{ print join ' ', @chars_seq; })) ] b b b aa character sequence:[b aa] --------------- target:[cccqrrtaaa] groups:[2 2 2] regex:[ (?: ( (.)\g{-1}{1} ) (?{ push @chars_seq, $^N; }) (?: (?=) | (?{ pop @ +chars_seq }) (*FAIL) ) (?{ print join ' ', @chars_seq; })) (?!\g{-1}) +.*? (?: ( (.)\g{-1}{1} ) (?{ push @chars_seq, $^N; }) (?: (?=) | (?{ pop @ +chars_seq }) (*FAIL) ) (?{ print join ' ', @chars_seq; })) (?!\g{-1}) +.*? (?: ( (.)\g{-1}{1} ) (?{ push @chars_seq, $^N; }) (?: (?=) | (?{ pop @ +chars_seq }) (*FAIL) ) (?{ print join ' ', @chars_seq; })) ] cc cc cc rr cc rr aa character sequence:[cc rr aa] --------------- target:[cccqrrtaaa] groups:[1 1 2 2] regex:[ (?: ( (.)\g{-1}{0} ) (?{ push @chars_seq, $^N; }) (?: (?=) | (?{ pop @ +chars_seq }) (*FAIL) ) (?{ print join ' ', @chars_seq; })) (?!\g{-1}) +.*? (?: ( (.)\g{-1}{0} ) (?{ push @chars_seq, $^N; }) (?: (?=) | (?{ pop @ +chars_seq }) (*FAIL) ) (?{ print join ' ', @chars_seq; })) (?!\g{-1}) +.*? (?: ( (.)\g{-1}{1} ) (?{ push @chars_seq, $^N; }) (?: (?=) | (?{ pop @ +chars_seq }) (*FAIL) ) (?{ print join ' ', @chars_seq; })) (?!\g{-1}) +.*? (?: ( (.)\g{-1}{1} ) (?{ push @chars_seq, $^N; }) (?: (?=) | (?{ pop @ +chars_seq }) (*FAIL) ) (?{ print join ' ', @chars_seq; })) ] c c c c q c q rr c q rr aa character sequence:[c q rr aa] --------------- target:[cccqrrtaaa] groups:[1 1 1 1 3] regex:[ (?: ( (.)\g{-1}{0} ) (?{ push @chars_seq, $^N; }) (?: (?=) | (?{ pop @ +chars_seq }) (*FAIL) ) (?{ print join ' ', @chars_seq; })) (?!\g{-1}) +.*? (?: ( (.)\g{-1}{0} ) (?{ push @chars_seq, $^N; }) (?: (?=) | (?{ pop @ +chars_seq }) (*FAIL) ) (?{ print join ' ', @chars_seq; })) (?!\g{-1}) +.*? (?: ( (.)\g{-1}{0} ) (?{ push @chars_seq, $^N; }) (?: (?=) | (?{ pop @ +chars_seq }) (*FAIL) ) (?{ print join ' ', @chars_seq; })) (?!\g{-1}) +.*? (?: ( (.)\g{-1}{0} ) (?{ push @chars_seq, $^N; }) (?: (?=) | (?{ pop @ +chars_seq }) (*FAIL) ) (?{ print join ' ', @chars_seq; })) (?!\g{-1}) +.*? (?: ( (.)\g{-1}{2} ) (?{ push @chars_seq, $^N; }) (?: (?=) | (?{ pop @ +chars_seq }) (*FAIL) ) (?{ print join ' ', @chars_seq; })) ] c c c c q c q r c q r c q r t c q r t aaa character sequence:[c q r t aaa]

In reply to How to access last capture groups outside regex using interpolation but w/o using re 'eval'? by rsFalse

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.