in reply to Re^3: How to replace greedy alternation?
in thread How to replace greedy alternation?
#!/usr/bin/perl use strict; use warnings; # If all four chars are present, returns them in first-seen order. # If any of the four are not present, returns an empty string. sub four_or_nothing { my $str = shift; my @in_order = ($str =~ m{ ([abcd]) .*? (?!\1)([abcd]) .*? (?!\1|\2)([abcd]) .*? (?!\1|\2|\3)([abcd]) }x); return join("", @in_order); } foreach my $case ( '1b22a7b3d3ccaacbcaccddeqcc11oabepd', '1b22w7b3d3ccsqcbc2ccddeqcc11o9bepd', ) { printf qq{%s: "%s"\n}, $case, four_or_nothing($case); } __DATA__ 1b22a7b3d3ccaacbcaccddeqcc11oabepd: "badc" 1b22w7b3d3ccsqcbc2ccddeqcc11o9bepd: ""
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: How to replace greedy alternation?
by gone2015 (Deacon) on Jan 29, 2009 at 13:26 UTC |