Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 101053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:41:49+00:00 2026-05-11T00:41:49+00:00

I’m probably doing this all wrong. I have a text file full of data

  • 0

I’m probably doing this all wrong. I have a text file full of data and I want to match and replace patterns of ‘item’ and ‘catalog number’ that are in the file. But the order of each element in the file is very important, so I want to match/replace starting from the top of the file and then work my way down.

The code snippet below actually works, but when I execute it, it replaces the third instance of the ‘SeaMonkey’ & ‘SMKY-1978’ pattern and then it replaces the second instance of that pattern. What I’d like it to do is replace the first instance of the pattern and then the second.

So I’d like the output to say ‘Found Kurt’s SMKY-1978 SeaMonkeys’ and then ‘Found Shane’s SMKY-1978 SeaMonkeys’ and then leave Mick’s SMKY-1978 SeaMonkeys alone since I only want to find and replace the first 2 instances of the pattern. Right now it says ‘Found Shane’s SMKY-1978 SeaMonkeys’ and ‘Found Mick’s SMKY-1978 SeaMonkeys’ because it is matching the last pattern each time the for loop is executed.

So am I missing a subtle little known regex character or am I just doing what I want to do completely and utterly wrong?

Here is the working code:

# my regexp matches from the bottom to the top but I'd like it to replace from the top down local $/=undef; my $DataToParse = <DATA>; my $item = 'SeaMonkeys'; my $catNum = 'SMKY-1978'; my $maxInstancesToReplace = 2; parseData(); exit();  sub parseData {     for (my $counter = 0; $counter < $maxInstancesToReplace; $counter++) {         # Stick in a temporary text placeholder that I will replace later after more processing         $DataToParse =~ s/(.+)\sELEMENT\s(.+?)\s\(Item := \'$item\'.+?CatalogNumber := \'$catNum.+?END_ELEMENT(.+)/$1 ***** Found $2\'s $catNum $item. (counter: $counter) *****$3/s;     }      print('Here's the result:\n$DataToParse\n'); }  __DATA__     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      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      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      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 

And here is what it currently outputs:

Here's the result:         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          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 Shane's SMKY-1978 SeaMonkeys. (counter: 1) *****          ELEMENT Kurt (Item := 'Battleship',                       ItemID := 99,                       CatalogNumber := 'BTLS-5234',                       Vendor := 529,         END_ELEMENT   ***** Found Mick's SMKY-1978 SeaMonkeys. (counter: 0) *****          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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:41:50+00:00Added an answer on May 11, 2026 at 12:41 am

    The best solution appears to be to grab each ELEMENT … END_ELEMENT section from the data and regex only one section at a time rather than feeding the whole complete data set to a regular expression at once. Not exactly what I was trying to accomplish, but I rewrote my program to do this piecemeal processing and it works like a charm.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JSP page retrieving data and when single or double quotes are
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.