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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:58:12+00:00 2026-06-13T12:58:12+00:00

This regular expression /\(.*\)/ won’t match the matching parenthesis but the last parenthesis in

  • 0

This regular expression

/\(.*\)/

won’t match the matching parenthesis but the last parenthesis in the string. Is there a regular expression extension, or something similar, with a proper syntax that allows for this? For example:

there are (many (things (on) the)) box (except (carrots (and apples)))

/OPEN(.*CLOSE)/ should match (many (things (on) the))

There could be infinite levels of parentheses.

  • 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-13T12:58:13+00:00Added an answer on June 13, 2026 at 12:58 pm

    If you only have one level of parentheses, then there are two possibilities.

    Option 1: use ungreedy repetition:

    /\(.*?\)/
    

    This will stop when it encounters the first ).

    Option 2: use a negative character class

    /\([^)]*\)/
    

    This can only repeat characters that are not ), so it can necessarily never go past the first closing parenthesis. This option is usually preferred due to performance reasons. In addition, this option is more easily extended to allow for escaping parenthesis (so that you could match this complete string: (some\)thing) instead of throwing away thing)). But this is probably rather rarely necessary.

    However if you want nested structures, this is generally too complicated for regex (although some flavors like PCRE support recursive patterns). In this case, you should just go through the string yourself and count parentheses, to keep track of your current nesting level.

    Just as a side note about those recursive patterns: In PCRE (?R) simply represents the whole pattern, so inserting this somewhere makes the whole thing recursive. But then every content of parentheses must be of the same structure as the whole match. Also, it is not really possible to do meaningful one-step replacements with this, as well as using capturing groups on multiple nested levels. All in all – you are best off, not to use regular expressions for nested structures.

    Update: Since you seem eager to find a regex solution, here is how you would match your example using PCRE (example implementation in PHP):

    $str = 'there are (many (things (on) the)) box (except (carrots (and apples)))';
    preg_match_all('/\([^()]*(?:(?R)[^()]*)*\)/', $str, $matches);
    print_r($matches);
    

    results in

    Array
    (
        [0] => Array
            (
                [0] => (many (things (on) the))
                [1] => (except (carrots (and apples)))
            )   
    )
    

    What the pattern does:

    \(      # opening bracket
    [^()]*  # arbitrarily many non-bracket characters
    (?:     # start a non-capturing group for later repetition
    (?R)    # recursion! (match any nested brackets)
    [^()]*  # arbitrarily many non-bracket characters
    )*      # close the group and repeat it arbitrarily many times
    \)      # closing bracket
    

    This allows for infinite nested levels and also for infinite parallel levels.

    Note that it is not possible to get all nested levels as separate captured groups. You will always just get the inner-most or outer-most group. Also, doing a recursive replacement is not possible like this.

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

Sidebar

Related Questions

I want to match D11-RONPLAYER_DEF_15_PO using this regular expression: D\[0-9]+-\[A-Z]*PLAYER_(DEF\[0-9]*)_(\[^_]+)_ but it does not
What's wrong with this regular expression? It won't work var patt = /[0-9]{2}/[0-9]{2}/[1-9]{4}/; if(patt.test(document.getElementById('date').value)
I want to use this regular expression in Python: <(?:[^]*[']*|'[^']*'[']*|[^'>])+> (from RegEx match open
I created this regular expression to validate names: ^[a-zA-Z0-9\s\-\,]+.\*?$ Is there a way add
How to limit string size for this regular expression? /^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/ I just need to
I'm having trouble with an Expect regular expression. I'm trying to match on this
Hopefully this won't be too difficult, but I'm not too skilled in regular expressions.
I need a Perl regular expression to match a string. I'm assuming only double-quoted
Is this regular expression enough to catch all cross site scripting attempts when embedding
How can I update this regular expression to find and replace not just /news/

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.