fork download
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $string = "111s11111s11111s";
  7.  
  8. sub skip_greedy {
  9. my ($string, $skips) = @_;
  10.  
  11. print "Greedy matching, skip $skips - ";
  12.  
  13. print $string =~ /^(.*)s(?(?{$skips-- > 0})(*FAIL))/
  14. ? "$1\n"
  15. : "no match\n";
  16. }
  17.  
  18. skip_greedy($string, $_) for (0..3);
  19.  
  20. sub all_matches {
  21. my ($string) = @_;
  22.  
  23. my @seqs;
  24. $string =~ /^(.*)s(?{push @seqs, $1})(*FAIL)/;
  25.  
  26. my @sorted = sort {length $b <=> length $a} @seqs;
  27.  
  28. return @sorted;
  29. }
  30.  
  31. use Data::Dumper;
  32.  
  33. print Dumper [ all_matches($string) ];
  34. print Dumper [ all_matches($string) ];
Success #stdin #stdout #stderr 0.03s 5184KB
stdin
Standard input is empty
stdout
Greedy matching, skip 0 - 111s11111s11111
Greedy matching, skip 1 - 111s11111s11111
Greedy matching, skip 2 - 111s11111s11111
Greedy matching, skip 3 - 111s11111s11111
$VAR1 = [
          '111s11111s11111',
          '111s11111',
          '111'
        ];
$VAR1 = [];
stderr
Variable "$skips" will not stay shared at (re_eval 1) line 1.
Variable "@seqs" will not stay shared at (re_eval 2) line 1.