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

Slurping files should generally be avoided, but your data seems to have a nice record separator so you can set $/ to that and then:

use strict; use warnings; my $item = "SeaMonkeys"; my $catNum = "SMKY-1978"; my $remainingToReplace = 2; my @lines; local $/ = "\n\n"; while (defined (my $block = <DATA>)) { @lines = split "\n", $block; next unless $remainingToReplace; next unless $lines[0] =~ m/\Q$item\E/; # Replacement needed here print "***** Found $item, counter $remainingToReplace *****\n"; --$remainingToReplace; } continue { print join "\n", @lines, "\n"; } __DATA__ Data per OP's node

Prints:

ELEMENT Kurt (Item := "BrightLite", ItemID := 29, CatalogNumber := "BTLT-9274", Vendor := 100, END_ELEMENT ELEMENT Mick (Item := "PetRock", ItemID := 36, CatalogNumber := "PTRK-3475/A", Vendor := 82, END_ELEMENT ***** Found SeaMonkeys, counter 2 ***** ELEMENT Kurt (Item := "SeaMonkeys", ItemID := 12, CatalogNumber := "SMKY-1978/E", Vendor := 77, END_ELEMENT ELEMENT Joe (Item := "Pong", ItemID := 24, CatalogNumber := "PONG-1482", Vendor := 5, END_ELEMENT ***** Found SeaMonkeys, counter 1 ***** ELEMENT Shane (Item := "SeaMonkeys", ItemID := 1032, CatalogNumber := "SMKY-1978/E", Vendor := 77, END_ELEMENT ELEMENT Kurt (Item := "Battleship", ItemID := 99, CatalogNumber := "BTLS-5234", Vendor := 529, END_ELEMENT ...

Perl reduces RSI - it saves typing

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

    Awesome! Thanks for the answer! This seems to work fine. I had to modify your suggested regex a bit to handle matching both the item and the catalog number, since that was part of my original problem. I may not have made that very clear above.

    My regex looks like this, and it's acting on the whole $block variable instead of acting on just the first line of each block.

    m/ELEMENT\s(.+?)\s\(Item := \"\Q$item\E\".+?CatalogNumber := \"\Q$catNum\E/s

    Thanks again for the quick help!

Re^2: can I make my regexp match first pattern instead of last?
by kleucht (Beadle) on Oct 25, 2008 at 19:47 UTC

    I have one small remaining problem, though. My data is already a large string variable instead of a filehandle. I used the filehandle in my example above because it was easy to just copy and paste. So what do I do with this "while" line in your solution if I already have the whole dataset slurped up into a variable called $fileContents?

    while (defined (my $block = <DATA>))

      Use the string as a file:

      my $fileContents; open my $scan, '<', \$fileContents; while (defined (my $block = <$scan>)) { ... } close $scan;

      Perl reduces RSI - it saves typing
        It's working! Thanks for the help!
        Using the string as a filehandle works fine. But now I get a runtime error that says "Can't use string ("") as a symbol ref while "strict refs" in use"

        on this line of code:

        open $ResultsHandle, '<', \$return;

        So how can I use strict, which is required by Perl Best Practices, and process a multi line string one line at a time?

        Thanks, Kurt