Regex Not Matching Complex Nested Parentheses in Python - guide with Performance
I'm stuck on something that should probably be simple. I'm working on a personal project and I'm optimizing some code but I've looked through the documentation and I'm still confused about I'm trying to use regex in Python to extract content from strings that can have nested parentheses... My current pattern is failing when faced with multiple levels of nested parentheses, and I'm working with important performance optimization. Here's what I've been using: ```python import re def extract_nested(text): pattern = r'\(([^()]*|(\([^()]*\))*)*\)' return re.findall(pattern, text) ``` When I run this on a string like `"This is (a test (with (nested)) parts) and (another)"`, I get unexpected results. The output is incomplete, and I’ve noticed it gets slower with longer strings containing deeper nesting. The regex seems to just stop matching after one level. I tried simplifying the regex, but it doesn't give me the nested structure I need. I also considered using recursion but found that it complicates things further. I’m currently using Python 3.9, and the regex pattern is taking much longer than I expected for larger inputs. Is there a better way to approach this question that still keeps performance in check? Any insights would be greatly appreciated! This is part of a larger service I'm building. Any ideas what could be causing this? Is there a better approach? How would you solve this? This is part of a larger web app I'm building. Any advice would be much appreciated. This is happening in both development and production on Windows 11. Thanks, I really appreciate it!