in reply to regex greedy range

Well, if you want to capture all the parts, then, well capture all the parts.

my @input = qw( A/B/C/D/E/F/ A/B/C/D/ A/B/C/D A/B/C/ A/B/C A/B A ); foreach (@input){ next if not m!((((\w+/)\w+/)\w+/)\w+/?)!; print "$_ has $4 $3 $2 $1\n"; }

Misread the question…

Makeshifts last the longest.

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

    You forgot a bunch of question marks, and you're using capturing when you only need grouping:

    m!((((\w+/)\w+/)\w+/)\w+/?)!;
    should be:
    m!((?:(?:(?:\w+/)?\w+/)?\w+/)?\w+/?)!;
    but that requires lots of backtracking, so I think it's less efficient than:
    m!(\w+(?:/\w+(?:/\w+(?:/\w+)?)?)?)!;
    which only requires a single character lookahead.

      No, I didn't forgot the question marks, and I used capturing parens on purpose. But I was answering a different question than was actually asked.

      It could actually turn out more efficient with a slight variation:

      m!((?>(?>(?>\w+/)?\w+/)?\w+/)?\w+/?)!;

      I haven't done any benchmarks though.

      Makeshifts last the longest.