fork(1) 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 = shift;
  10. local our $skips = shift;
  11.  
  12. print "Greedy matching, skip $skips - ";
  13.  
  14. print $string =~ /^(.*)s(?(?{$skips-- > 0})(*FAIL))/
  15. ? "$1\n"
  16. : "no match\n";
  17. }
  18.  
  19. skip_greedy($string, $_) for (0..3);
  20.  
  21. sub all_matches {
  22. my ($string) = @_;
  23.  
  24. local our @seqs;
  25. $string =~ /^(.*)s(?{push @seqs, $1})(*FAIL)/;
  26.  
  27. my @sorted = sort {length $b <=> length $a} @seqs;
  28.  
  29. return @sorted;
  30. }
  31.  
  32. use Data::Dumper;
  33.  
  34. print Dumper [ all_matches($string) ];
  35. print Dumper [ all_matches($string) ];
Success #stdin #stdout 0.03s 5224KB
stdin
Standard input is empty
stdout
Greedy matching, skip 0 - 111s11111s11111
Greedy matching, skip 1 - 111s11111
Greedy matching, skip 2 - 111
Greedy matching, skip 3 - no match
$VAR1 = [
          '111s11111s11111',
          '111s11111',
          '111'
        ];
$VAR1 = [
          '111s11111s11111',
          '111s11111',
          '111'
        ];