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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:24:34+00:00 2026-06-07T20:24:34+00:00

I have a file that is structured in a large multidimensional structure, similar to

  • 0

I have a file that is structured in a large multidimensional structure, similar to json, but not close enough for me to use a json library.

The data looks something like this:

alpha {
    beta {
        charlie;
    }
    delta;
}

echo;
foxtrot {
    golf;
    hotel;
}

The regex I am trying to build (for a preg_match_all) should match each top level parent (delimited by {} braces) so that I can recurse through the matches, building up a multidimensional php array that represents the data.

The first regex I tried is /(?<=\{).*(?=\})/s which greedily matches content inside braces, however this isn’t quite right as when there is more than one sibling in the top level the match is too greedy. Example below:

Using regex /(?<=\{).*(?=\})/s match is given as:

Match 1:

    beta {
        charlie;
    }
    delta;
}

echo;
foxtrot {
    golf;
    hotel;

Instead the result should be:
Match 1:

    beta {
        charlie;
    }
    delta;

Match 2:

    golf;
    hotel;

So regex wizards, what function am I missing here or do I need to solve this with php somehow? Any tips very welcome 🙂

  • 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-07T20:24:35+00:00Added an answer on June 7, 2026 at 8:24 pm

    You can’t 1 do this with regular expressions.

    Alternatively, if you want to match deep-to-shallow blocks, you can use \{[^\{\}]*?\} and preg_replace_callback() to store the value, and return null to erase it from the string. The callback will need to take care of nesting the value accordingly.

    $heirarchalStorage = ...;
    do {
        $string = \preg_replace_callback('#\{[^\{\}]*?\}#', function($block)
        use(&$heirarchalStorage) {
            // do your magic with $heirarchalStorage
            // in here
            return null;
        }, $string);
    } while (!empty($string));
    

    Incomplete, not tested, and no warranty.

    This approach requires that the string be wrapped in {} as well, otherwise the final match won’t happen and you’ll loop forever.

    This is an awful lot of (inefficient) work for something that can just as easily be solved with a well known exchange/storage format such as JSON.

    1 I was going to put “you can, but…“, however I’ll just say once again, “You can’t” 2

    2 Don’t

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

Sidebar

Related Questions

So I have a plist structured string, that get dynamically (not from the file
I have several large zip file that contain a dir structure that I must
I have a large file that contains a particular structure.I want to know the
I have a linux utility that parses structured input ( a text file ),
I have a file structure that looks like this ./501.res/1.bin ./503.res/1.bin ./503.res/2.bin ./504.res/1.bin and
I have an xml file that contains a structure.. In this structure, I have
I have a file from bank that is structure in very special way. Where
I have an XML file format that contains a structure of questions: <question id=q101>
I have a pretty large file that I need to parse in php, so
So I have a Python file that contains a large set of data of

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.