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 6098197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:07:50+00:00 2026-05-23T13:07:50+00:00

I have a task to accomplish, and I’ve been trying with regex for hours

  • 0

I have a task to accomplish, and I’ve been trying with regex for hours to no avail. A little help and some teaching would be appreciated. The .ini file format is:

ES=Spanish
EN=English
TOKEN=Token
FILES=Files

The goal of my routine is to

  1. Get a key that needs to be changed,
  2. Figure out what the current value of that key is,
  3. add some comments about the old value and who changed it,
  4. change the value.

So, for example, if I want to change the value belonging to EN from English to Eigo, I would end up with:

ES=Spanish
#Changed by Jane Doe on <date>
#Old value: EN=English
EN=Eigo
TOKEN=Token
FILES=Files

My code:

        $content = "EN=English\n"
            . "ES=Spanish\n"
            . "TOKEN=Token\n"
            . "FILES=Files\n";

            $key = 'EN';
            $newValue = 'Eigo";
            $editor = 'Jane Doe';

             //get the old value
    $matches = array();
    preg_match('~'.$key.'\s?=[^\n$]+~iu',$content,$matches);

    $commentLines = '# Change by ' . $editor . ' on ' . date("m/d/Y g:i a") . "\n";
    $commentLines .= '# Old value: ' . $matches[0] . "\n";
    $newValueLine = $key.'='.$newValue. "\n";
    $newEntry = $commentLines . $newValueLine;
    $content = preg_replace('~'.$key.'\s?=[^\n$]+~iu',$newEntry,$content);

This was working fine, until I realized that if someone changed the key for a short string, like EN, then my regex matches the EN in TOKEN, and also changes that, but messes up the whole file:

TOK#Changed by ....

So, a few questions:

  1. Because each KEY should be unique, should I even be using regex for this? Is there a faster/cleaner/better method I should be using?

  2. Why when I add a ^ to the regex on the line with preg_replace doesn’t this match the beginning of the line and get rid of my TOKEN matching problem?

    preg_replace('~^'.$key.'\s?=[^\n$]+~iu',$newEntry,$content)
    
  • 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. Editorial Team
    Editorial Team
    2026-05-23T13:07:51+00:00Added an answer on May 23, 2026 at 1:07 pm

    The information you give is a bit vague. Is this being stored in a file? If so, it seems like what you really want is version control software like subversion. It will keep track of what the state of the code/ini files were like at any point in time, who made a change, and if people want to put in a message about what they were doing, it will handle that as well.

    Your regex seems a bit overcomplicated. And for multiline search and replace, you need to use the m modifier.

    Try:

    $key = 'EN';
    preg_replace('~^'.$key.'=.*$~m', $newEntry, $content);
    

    Tested at http://www.spaweditor.com/scripts/regex/index.php with:

    Regex: 
        /^EN=.*$/m
    
    Data:
        EN=English
        ES=Spanish
        TOKEN=Token
        FILES=Files
    
    Replace: 
        FOO=bar
    
    Function:
        preg_replace
    
    Result:
        FOO=bar
        ES=Spanish
        TOKEN=Token
        FILES=Files
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to accomplish the following ridiculous task: I have a text file containing
I have a task and I want to generate some code using the CodeDom.
Here's what I'm trying to do : I have an entity Task with a
I have some slow internet task to save and load file, I'd like to
I have a Rakefile with a Rake task that I would normally call from
I have a simple task to accomplish, but I am not sure what is
I'm trying to accomplish a task and turns out that the code I need
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
I have a Task object that has a collection of Label objects ... in
I'm relatively new to Nant, what i'd like to do is have a task

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.