in reply to pattern match of first occurance

Your match (.+) is greedy so it will match as much as possible, i.e. up to the second </year>. Make it non-greedy with the addition of a question mark.

$ perl -E ' > $var = q{<x><year>2010<day>5</day></year><month>January</month> <day +>31</day>, <year>2010</year></x>}; > say $1 if $var =~m{<year>(.+?)</year>};' 2010<day>5</day> $

I hope this is helpful.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: pattern match of first occurance
by rdfield (Priest) on Jul 08, 2010 at 13:57 UTC
    Or match only digits:
    say $1 if $var =~m{<year>(\d+)</year>};

    rdfield

      Comment: Nope he said he's trying to get:
      2010<day>5</day>



      Demize
        Actually, no, he's not. The question was specifically why <year>.*</year> didn't match properly.

        rdfield