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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:44:21+00:00 2026-06-18T07:44:21+00:00

Possible Duplicate: Matching Nested Structures With Regular Expressions in Python I can’t wrap my

  • 0

Possible Duplicate:
Matching Nested Structures With Regular Expressions in Python

I can’t wrap my head around this problem. I have a string like the following one:

Lorem ipsum dolor sit amet [@a xxx yyy [@b xxx yyy [@c xxx yyy]]] lorem ipsum sit amet

My task would be to extract the commands (they are always starting with [@ and ending with ]) and their subcommands. A result like

[
    [@a xxx yyy [@b xxx yyy [@c xxx yyy]]], # the most outer
    [@b xxx yyy [@c xxx yyy]],              # the middle one
    [@c xxx yyy]                            # the inner most
]

would be highly appreciated. The problem is that these kind of commands can occur in very long text messages, so a “performant” solution would be nice.

I was toying around with some regex patterns mostly of the time something like

(\[@.*?\]\s) # for the outer one

but i have seen no light in matching the middle and inner one. To make it more complicated, the amount of nested commands is variable…
Might some special regex be the solution? I have read about lookaheads and lookbehinds but no idea how to use them in this special case.

Thank a bunch!

UPDATE

@Cyborgx37 pointed me to another post that uses the pyparsing package. It would be nice to have a solution without an external package or library. But pyparsing definately solves that problem!

  • 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-18T07:44:23+00:00Added an answer on June 18, 2026 at 7:44 am

    C# has recursive/nested RegEx, I don’t believe Python does. You could re-run the RegEx search on previous results, but this is probably less efficient (the overhead of RegEx for such a simple search) than just making a custom parser. The text your searching for “[@” and “]” isn’t very complex.

    Here’s a custom parser (in JavaScript) that would do the job.

    var txt = "Lorem ipsum dolor sit amet [@a xxx yyy [@b xxx yyy [@c xxx yyy]]] lorem ipsum sit amet";
    function parse(s) {
        var stack = [];
        var result = [];
        for(var x=0; x<s.length; x++) {
            var c = s.charAt(x);
            if(c == '[' && x+1 < s.length-1 && s.charAt(x+1) == '@') {
                for(var y=0; y<stack.length; y++)
                    stack[y] += "[@";
                stack.push("[@");
                x++;
            } else if(c == ']' && stack.length > 0) {
                for(var y=0; y<stack.length; y++)
                    stack[y] += "]";
                result.push(stack.pop());
            } else {
                for(var y=0; y<stack.length; y++)
                    stack[y] += c;
            }
        }
        return result;
    }
    parse(txt);
    

    It quickly loops through all the characters of the text (only once) and uses a stack and an if…if else…else condition to push, pop and modify the values in that stack respectively.

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

Sidebar

Related Questions

Possible Duplicate: Matching Nested Structures With Regular Expressions in Python I am trying to
Possible Duplicate: matching unicode characters in python regular expressions Using re.findall(r'\w+', ip) on Fältskog
Possible Duplicate: Python Regular Expression Matching: ## ## I already asked this question, but
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: How can I use inverse or negative wildcards when pattern matching in
Possible Duplicate: Regular Expression matching for entire string On my form page, I am
Possible Duplicate: R regular expression: http matching I'm working to capture URLs from a
Possible Duplicate: Reversing a regular expression in python I think I ran into a
Possible Duplicate: Regex for matching javadoc fragments I have files having content like /**

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.