Related to another conversation, this regular expression pattern will help determine if a JSON string is balanced. I used free-space mode with comments because, otherwise, it just looks like a bunch of punctuation marks.
(?x) # FREE SPACE MODE
# BEGIN - Match a JSON object
(
\\{ # Opening brace
([^\\[{\\]}]|(?R))*
\\} # Closing brace
)
# END
|
# BEGIN - Match a JSON array
(
\\[ # Opening bracket
([^\\[{\\]}]|(?R))*
\\] # Closing bracket
)
# END
As code without comments:
dim rx as new RegEx
rx.SearchPattern = _
"(?xmi-Us) (\\{ ([^\\[{\\]}]|(?R))*\\} )|(\\[ ([^\\[{\\]}]|(?R))*\\] )"
dim rxOptions as RegExOptions = rx.Options
rxOptions.LineEndType = 4
dim match as RegExMatch = rx.Search( sourceText )