in reply to regular expression and nested tags

What is going on with the not greedy regexpr?

Non-greedy stops at earliest position where the pattern will match (before the first </div>).
Greedy stops at latest position where the pattern will match (before the last </div>).

Replies are listed 'Best First'.
Re^2: regular expression and nested tags
by vitoco (Hermit) on Jun 12, 2009 at 21:03 UTC

    Just thought that starting at <div> after B would result in a better nongreedy result.

    Is there a way to match a string contained between two delimiters which does not contain a given word? (not a class)

      No, it only affects /.*/. It doesn't affect any other part of the match, namely, the earlier /<div>/.

      Is there a way to match a string contained between two delimiters which does not contain a given word? (not a class)

      The following would do here:
      m{<div>(?:(?!</div>).)*</div>}

        That doesn't work. I must be doing something wrong:

        #!perl $a = 'A<div>B<div>C<div>D</div>E</div>F</div>G'; $a =~ m%<div>((?:(?!</div>).)*)</div>%; print "=$1=\n";

        I want to get just =D=. Also tried variations of this advanced patterns like look-behind without success.