CodexBloom - Programming Q&A Platform

Regex to Match Nested Parentheses - Struggles with Depth and Performance

πŸ‘€ Views: 105 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-03
regex python performance Python

I'm collaborating on a project where I'm relatively new to this, so bear with me. I'm currently working on a project in Python 3.9 where I need to extract content from strings that contain nested parentheses. For example, given the string "(abc(def(ghi)jkl)mno)pqr", I need to match the entire segments enclosed by the outermost parentheses, which should return the whole string since the outer parentheses are not broken by any unmatched ones. I've tried using a regex pattern like `\(([^()]*|\([^()]*\))*\)` to achieve this, but I'm running into issues where it either fails to match the expected segments or takes an incredibly long time to process when the nesting gets deeper. For instance, with a string like "(a(b(c(d)e)f)g)h(i(j(k(l(m)n)o)p)q)r)", the regex seems to hang indefinitely or return incomplete matches. Here’s the code snippet I've been working with: ```python import re pattern = r'\(([^()]*|\([^()]*\))*\)' test_strings = [ '(abc(def(ghi)jkl)mno)pqr', '(a(b(c(d)e)f)g)h(i(j(k(l(m)n)o)p)q)r)', ] for string in test_strings: match = re.search(pattern, string) print(f'Matching for: {string} => {match.group(0) if match else "No match"}') ``` What I ideally want is a regex that can accurately and efficiently handle various levels of nested parentheses without compromising performance. Are there better approaches to achieve this, or am I fundamentally misusing regex for nested structures? I appreciate any insights, especially regarding potential pitfalls and best practices for regex performance in this context. Any ideas what could be causing this? Is there a simpler solution I'm overlooking? What am I doing wrong?