c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; use warnings; use strict; ;; use Test::More 'no_plan'; use Test::NoWarnings; ;; use Data::Dump qw(dd pp); ;; my @TESTS = ( [ '', [ 1, ], [ ], ], [ 'c', [ 1, ], [ qw(c) ], ], [ 'c', [ 2, ], [ ], ], [ 'cba', [ 1, ], [ qw(c) ], ], [ 'cbaa', [ 1, 2, ], [ qw(c aa) ], ], [ 'bbaa', [ 1, 2, ], [ qw(b aa) ], ], [ 'cccqrrtaaa', [ 1, 1, 1, 1, 3, ], [ qw(c q r t aaa) ], ], [ 'cccqrrtaaa', [ 2, 2, 2, ], [ qw(cc rr aa) ], ], [ 'cccqrrtaaa', [ 1, 1, 2, 2, ], [ qw(c q rr aa) ], ], ); ;; VECTOR: for my $ar_vector (@TESTS) { if (not ref $ar_vector) { note $ar_vector; next VECTOR; } ;; my ($string, $ar_groups, $ar_expected) = @$ar_vector; ;; my $rx = build_regex($ar_groups); ;; my @groups = extract_groups($string, $rx); ;; is_deeply \@groups, $ar_expected, qq{'$string' <- (@$ar_groups) -> (@$ar_expected)} } ;; done_testing; ;; exit; ;; ;; sub build_regex { my ($ar_groups) = @_; ;; my ($rx) = map qr{ $_ }xms, join ' .*? ', map qr{ ((.) \g-1{$_} (?! \g-1)) }xms, map $_-1, @$ar_groups ; ;; return $rx; } ;; sub extract_groups { my ($string, $rx) = @_; ;; my $p; return grep $p = !$p, $string =~ $rx ; } " ok 1 - '' <- (1) -> () ok 2 - 'c' <- (1) -> (c) ok 3 - 'c' <- (2) -> () ok 4 - 'cba' <- (1) -> (c) ok 5 - 'cbaa' <- (1 2) -> (c aa) ok 6 - 'bbaa' <- (1 2) -> (b aa) ok 7 - 'cccqrrtaaa' <- (1 1 1 1 3) -> (c q r t aaa) ok 8 - 'cccqrrtaaa' <- (2 2 2) -> (cc rr aa) ok 9 - 'cccqrrtaaa' <- (1 1 2 2) -> (c q rr aa) 1..9 ok 10 - no warnings 1..10