Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: pattern match of first occurance

by johngg (Canon)
on Jul 08, 2010 at 10:41 UTC ( [id://848670]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://848670]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-26 05:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found