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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 157k
  • Answers 157k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It seems like you can technically create a library definition… May 12, 2026 at 11:02 am
  • Editorial Team
    Editorial Team added an answer Try to install Resharper (http://www.jetbrains.com/resharper/index.html) it gives you a lot… May 12, 2026 at 11:02 am
  • Editorial Team
    Editorial Team added an answer It does not seem to be directly available. The idea… May 12, 2026 at 11:02 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.