Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: can I make my regexp match first pattern instead of last?

by dreadpiratepeter (Priest)
on Oct 24, 2008 at 21:17 UTC ( [id://719435]=note: print w/replies, xml ) Need Help??


in reply to can I make my regex match first pattern instead of last?

Off the top of my head, it looks like you need to replace the first (.+) expression in your regex with (.+?) so that it doesn't eat all the text up until the last ELEMENT that follows it.


-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."

Replies are listed 'Best First'.
Re^2: can I make my regexp match first pattern instead of last?
by kleucht (Beadle) on Oct 24, 2008 at 21:24 UTC
    I actually tried that. It really got things honked up. Here is the output just by adding that single question mark to the first (.+) expression in the regex:
    Here's the result: ELEMENT Kurt (Item := "BrightLite", ItemID := 29, CatalogNumber := "BTLT-9274", Vendor := 100, END_ELEMENT ***** Found Mick (Item := "PetRock", ItemID := 36, CatalogNumber := "PTRK-3475/A", Vendor := 82, END_ELEMENT ***** Found Kurt's SMKY-1978 SeaMonkeys. (counter: 0) ***** ELEMENT Joe (Item := "Pong", ItemID := 24, CatalogNumber := "PONG-1482", Vendor := 5, END_ELEMENT ELEMENT Shane's SMKY-1978 SeaMonkeys. (counter: 1) ***** ELEMENT Kurt (Item := "Battleship", ItemID := 99, CatalogNumber := "BTLS-5234", Vendor := 529, END_ELEMENT ELEMENT Mick (Item := "SeaMonkeys", ItemID := 8, CatalogNumber := "SMKY-1978/F", Vendor := 77, END_ELEMENT ELEMENT Frank (Item := "PetRock", ItemID := 42, CatalogNumber := "PTRK-3475/B", Vendor := 82, END_ELEMENT ELEMENT Joe (Item := "SeaMonkeys", ItemID := 8, CatalogNumber := "SMKY-1979/A", Vendor := 77, END_ELEMENT
      Well, that is becuase your expression doesn't do what you think it does. the (.+?) sections you have are matching a lot more than you think they are going to. You start the match in one ELEMENT and eat through multiple records until you find your item name.
      As another poster suggested, you should first break the input up into records, the apply your regexes to that particular record.


      -pete
      "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
        Okay. That's why I'm asking what I'm doing wrong. I was under the incorrect impression that if I put in that question mark, then it would shrink the match down to just the minimum chunk of data that matches the pattern, not find the first ELEMENT text then eat through multiple records to find my item name. Regex works differently than I have read about in books and online, apparently.
        Okay. That's why I'm asking what I'm doing wrong. I was under the incorrect impression that if I put in that question mark, then it would shrink the match down to just the minimum chunk of data that matches the pattern, not find the first ELEMENT text then eat through multiple records to find my item name. Regex works differently than I have read about in books and online, apparently.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://719435]
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: (2)
As of 2024-04-25 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found