Regex scenarios to Capture Nested Parentheses in Python - Need guide
After trying multiple solutions online, I still can't figure this out. I've been banging my head against this for hours. I'm having a hard time understanding I'm working on a project and hit a roadblock. I'm working on a Python project where I need to extract expressions from a string that includes nested parentheses. I'm currently using the regex pattern `\(([^()]*|(?R))*\)` with the `regex` module (v2023.01.01). However, when I run the following code, it fails to match nested structures correctly. ```python import regex text = "Here is a string with (nested (parentheses) (inside)) and (another (example))." pattern = r'\(([^()]*|(?R))*\)' matches = regex.findall(pattern, text) print(matches) ``` I expected to get the full matches of the nested parentheses, but I'm only getting the outermost layers. The output is: ``` ['nested (parentheses)', 'inside', 'another (example)'] ``` I’ve also tried simplifying the pattern and using just `(\([^()]*\))`, but that only captures the immediate inner parentheses without the nested structure. The regex seems to ignore deeper levels. Is there a different way to structure my regex to capture all levels of nested parentheses? Any help with this would be greatly appreciated! My development environment is Ubuntu. How would you solve this? I'm working with Python in a Docker container on Windows 11. Could this be a known issue? Any examples would be super helpful.