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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:14:12+00:00 2026-06-12T18:14:12+00:00

I am trying to do a preg_replace so that i can format some text

  • 0

I am trying to do a preg_replace so that i can format some text outputted from another program that doesnt look exactly how i want it to ….

here is a the snippet….

zzz:xxx1 _:somewhereElse_1 ;
xxx:yyy "dddd" ;
zzz:xxx2 _:somewhereElse_2 ;
yyy:zzz "ffff" ;   

_:somewhereElse_1
    I am somewhere else ;
 _:somewhereElse_2
    whatsup ;

What i am trying to do is make it look like this…

zzz:xxx1 I am somewhere else ;
xxx:yyy "dddd" ;
zzz:xxx2 whatsup ;
yyy:zzz "ffff" ;   

I have been trying to do it in various stages…

Stage 1. Extract the _:identifier from the top bit…

preg_match_all("/.*:.*_:somewhereElse_[(0-9)]+/", $data, $matches);

That works
But what I cant do is extract the corresponding value from the bottom bit…
none of my dam regular expressions work.

If you bear in mind that i have extracted all the somewhereElse_n the closest i can get is

preg_match_all("/(\n$somewhereElse_n\n.*;)?/s", $data, $matches);

The problem is the matches array as loads of empty places (nearly 2000) and its matching all the somewhereElse’s instead of stopping after the first ..

      [1149] => 
      [1150] => 
      [1151] => 
      [1152] => 
         _:somewhereElse_1
            I am somewhere else ;
            So am i;
         _:somewhereElse_2
            whatsup ;
      [1153] => 
      [1154] => 

Any help would be gratefully accepted 😀

  • 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-06-12T18:14:13+00:00Added an answer on June 12, 2026 at 6:14 pm

    Your .* (any amount of anything) is “greedy”, and tries to match as much as it possibly can while still being valid. As such, it’s matching from the first match all the way to the last one.

    Changing it to .*? will make it non-greedy, which makes it match as little as possible while still remaining valid. As such, it should then match each individual occurrence as a separate match, instead of bundling them all into one.

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

Sidebar

Related Questions

I'm trying to figure out an preg_replace() (php) style function that I can use
$count_sql = preg_replace(/SELECT(.+?)FROM/, SELECT COUNT(*) FROM, $sql); It's probably pretty obvious what I'm trying
I'm trying to use preg_match to extract info from href=domain.com/subdir/?key=value The info I want
I'm having problems with regular expressions that I got from regexlib. I am trying
I'm trying to do a bbcode parser class that can create personalyzed tags, but
I want to make it so that in my chat-application, links to websites can
I'm trying to affilliateize some legacy text in a Django webapp. It's a pretty
I have a problem with the preg_replace function, I am trying to convert some
I am trying to sanitize a string so that it can be used to
I have this output(from another system) that I need the Test field to be

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.