Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression Negate Colon doesn't work

Tags:

java

regex

I have a JSON String which is:

{"dependencies":["xz","pkg-config","glib","gobject-introspection"],"conflicts_with":[],"caveats":null,"options":[{"option":"--universal","description":"Build a universal binary"}]}

And I wrote a regular expression to find the array behind "dependencies":

(?<=\"dependencies\":).*[^:](?=,)

in Java:

"(?<=\\\"dependencies\\\":).*[^:](?=,)"

However the result turns out:

["xz","pkg-config","glib","gobject-introspection"],"conflicts_with":[],"caveats":null,"options":[{"option":"--universal"

And only the last colon was excluded.

Please help me out.

like image 591
spacegoing Avatar asked Feb 15 '26 23:02

spacegoing


2 Answers

I suggest using this regex:

(?<=\"dependencies\":).[^:]*(?=,)

Or, almost equal:

(?<=\"dependencies\":)[^:]+(?=,)
like image 54
Wiktor Stribiżew Avatar answered Feb 18 '26 22:02

Wiktor Stribiżew


You could use a non-greedy zero-or-more quantifier:

(?<=\"dependencies\":)\[(.*?)\]

This would match ["xz","pkg-config","glib","gobject-introspection"] in the provided JSON.

like image 36
Marchev Avatar answered Feb 18 '26 22:02

Marchev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!