in reply to regex greedy range

Going from
    print "$1\n" if m|((?:\w+/){4})|;
to
    print "$1\n" if m|((?:\w+/){0,4})|;
gets you halfway there. Add a '?':
    print "$1\n" if m|((?:\w+/?){0,4})|;
and there you are.

'{4}' means match exactly 4 times, whereas '{0,4}' means match up to 4 times. The '?' makes the '/' optional.

Replies are listed 'Best First'.
Re^2: regex greedy range
by Aristotle (Chancellor) on Sep 16, 2004 at 23:55 UTC

    Except now all parts of your regex are optional so it will match even the empty string. Also, quantified parens containing only quantified terms are a recipe for eventual disaster. I'd rather write this like so:

    m|(\w+(?:/\w+){1,3})|

    Makeshifts last the longest.