I have a string of the form
_statistics =
[
{ Some JSON text here },
.
.
.
];
Basically, I need the text within '[' and '];' . How can i isolate this using RegEx match.
I have a string of the form
_statistics =
[
{ Some JSON text here },
.
.
.
];
Basically, I need the text within '[' and '];' . How can i isolate this using RegEx match.
This isn't really the best solution, but anything more intensive and you might as well be using a full JSON parsing library.
(?<=\[)[^\]]+(?=\])
This looks behind for an open bracket ((?<=\[)
), then matches 1+ non bracket characters ([^\]]+
), and looks ahead for the closing bracket (?=\])
. You could optionally forget the lookarounds, and use a capture group instead:
\[([^\]]+)\]
The reason this isn't the best solution, is because it literally looks for text between [
and ]
. So JSON like [ { "string" : "[I'm in a bracket]" } ]
would return { "string" : "[I'm in a bracket
as a match.
Example: Regex101