in reply to regex finding shortest string containing n of $c

It is not 'obvious' why the string should be bound by x's on both sides if you want the longest string. For n x's, the longest strings would seem to be
n = 1 - x 2 - xx 3 - xxx. 4 - xxx.x.... 5 - xxx.x.....x......
in other words, don't forget the trailing non-x caharacters to lengthen the string.

A regex to capture these strings is

my $regex = '\A' . ('[^x]*x' x $n) . '[^x]*'; $foo =~ m{ ($regex) }xms; # PBP orthodoxy :)
Update: With the problem redefined, the new regex should be retooled to use parsimonious quantifiers to find the shortest strings:
my $regex = '\A' . ('[^x]*?x' x $n);
Here I have also left off the last piece of the regex, as it is a noop.

-Mark

Replies are listed 'Best First'.
Re^2: regex finding longest string containing n of $c
by xipho (Scribe) on Sep 01, 2005 at 04:10 UTC
    That will work (with a little modification for the revamped question too). Hadn't thought of just repeating the search letter with * between- simple now that I see it. Thanks!

      A regex is not going to do it for you because it will find the first match regardless of the length of the match. If you need to find the shortest (or longest) match then you need to find all the matches and select the best. A regex on it's own generally doesn't do that.


      Perl is Huffman encoded by design.
        Since I want to start at the begining of $foo, and can use *? between $c, Mark's updated solution works as desired.