Regex scenarios to Extract Nested JSON Objects in Python - guide Needed with Complex Structures
I've spent hours debugging this and I'm working on a personal project and I'm trying to extract nested JSON objects from a string using regex in Python, but I'm running into issues with more complex structures. Specifically, my input string can contain several levels of nested JSON, and the regex I have currently only captures the outermost object. Here's a simplified version of the string I'm working with: ```python input_string = '{"data": {"user": {"id": 1, "name": "John"}, "comments": [{"text": "Great post!", "likes": 5}, {"text": "Thanks for sharing!", "likes": 2}]}}}' ``` The regex pattern I've been using is: ```python pattern = r'({.*?})' ``` When I run the following code: ```python import re matches = re.findall(pattern, input_string) print(matches) ``` I only get the outermost JSON object as a result: ``` ['{"data": {"user": {"id": 1, "name": "John"}, "comments": [{"text": "Great post!", "likes": 5}, {"text": "Thanks for sharing!", "likes": 2}]}}'] ``` I need to capture not just the outermost object, but also the nested structures within it. I've tried modifying my regex to account for nesting by using more complex patterns like `r'({(?:[^{}]|(?R))*})'`, but this raises a `ValueError: too many values to unpack` behavior. Is there a way to write a regex pattern that can effectively capture nested JSON objects in Python without hitting recursion limits or causing parsing issues? Any help or suggestions on how to approach this would be greatly appreciated! Any help would be greatly appreciated! I'm working with Python in a Docker container on CentOS. Cheers for any assistance!