Python removes JSON substring

If I have a line where there is a valid JSON substring like this:

mystr = '100{"1":2, "3":4}312' 

What is the best way to extract only a JSON string? Numbers outside can be any (except { or } ), including newlines, etc.

To be clear, this is the result I want

  newStr = '{"1":2, "3":4}' 

The best way I can think of is to use find and rfind and then take a substring. This seems too verbose for me, and it is not compatible with python 3.0 (which I would prefer but not be significant)

Any help is appreciated.

+4
source share
1 answer

Note that in the following code there are a lot of assumptions that on each side of the JSON string there is nothing but a parenthesis.

 import re matcher = re.compile(r""" ^[^\{]* # Starting from the beginning of the string, match anything that isn't an opening bracket ( # Open a group to record what next \{.+\} # The JSON substring ) # close the group [^}]*$ # at the end of the string, anything that isn't a closing bracket """, re.VERBOSE) # Your example print matcher.match('100{"1":2, "3":4}312').group(1) # Example with embedded hashmap print matcher.match('100{"1":{"a":"b", "c":"d"}, "3":4}312').group(1) 

Short, non-precompiled, non-commented version:

 import re print re.match("^[^\{]*(\{[^\}]+\})[^}]*$", '100{"1":2, "3":4}312').group(1) 

Although regular-comment comments are very preferable to maintain service.

+6
source

Source: https://habr.com/ru/post/1380424/


All Articles