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)
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.
source share