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

s/// usually works from the top, you must be doing to change that.
$DataToParse =~ s/(.+)\sELEMENT\s(.+?)\s\(Item := \"$item\".+? +CatalogNumber := \"$catNum.+?END_ELEMENT/$1 ***** Found $2\'s $catNum + $item. (counter: $counter) *****$3/s;
Ah, yes. Drop the useless initial subpattern (.+) (you only reproduce what it captures anyway) and you'll be much closer to home. Capturing the rest of the string after what you care for, is unnecessary too, for the same reason.

A regex doesn't have to match a whole string, you know.

$DataToParse =~ s/\sELEMENT\s(.+?)\s\(Item := \"$item\".+?Cata +logNumber := \"$catNum.+?END_ELEMENT/ ***** Found $1\'s $catNum $item +. (counter: $counter) *****/s;

Replies are listed 'Best First'.
Re^2: can I make my regex match first pattern instead of last?
by kleucht (Beadle) on Oct 25, 2008 at 20:53 UTC

    That doesn't do it either. It's matching more than just one single ELEMENT block. Here's the output when I put in your suggestion (although I think I changed a catalog number in the data since my original post):

    Here's the result: ***** Found 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 ELEMENT Kurt's SMKY-1978 SeaMonkeys. (counter: 0) ***** ELEMENT Kurt (Item := "Battleship", ItemID := 99, CatalogNumber := "BTLS-5234", Vendor := 529, END_ELEMENT ELEMENT Mick's SMKY-1978 SeaMonkeys. (counter: 1) ***** ELEMENT Frank (Item := "PetRock", ItemID := 42, CatalogNumber := "PTRK-3475/B", Vendor := 82, END_ELEMENT ELEMENT Joe (Item := "SeaMonkeys", ItemID := 8, CatalogNumber := "SMKY-1978/A", Vendor := 77, END_ELEMENT