Hiro Protagonist Hiro Protagonist - 11 days ago 7
JSON Question

Capture repeating elements between two words in JSON using RegEx

I have a web service response, returning JSON, that I need to parse. I would like to capture all instances of

"id":"123-abc-345"
between a starting word and an ending word. I need to know all IDs so that I can randomly choose between them. The reason I have to look between the "key beginning word" and "key ending word" is that the ID element appears in the document in various places (even before the beginning and ending words), but I'm just interested in the IDs between "beginning" and "ending".

Example data I'm using:

[]}6778:---esghsrth"id":"95907bc09-568976456-c6a5a-4f87g"[]}6778:---AAAAA{[]}bla...esghshrth"id":"95907bc09-568976456-c6a5a-4f87g"[]}6778:---esghsrth"id":"95907bc09-568976456-c6a5a-4f87g"[]}6778:---esghsrth"id":"95907bc09-568976456-c6a5a-4f87g"[]}6778:---esghsrth"id":"95907bc09-568976456-c6a5a-4f87g"[]}6778:---esghsrth"id":"95907bc09-568976456-c6a5a-4f87g"[]}6778:---esghsrth"id":"95907bc09-568976456-c6a5a-4f87g"[]}6778:---esghsrth"id":"95907bc09-568976456-c6a5a-4f87g"[]}6778:---ZZZZZ[]}6778:---esghsrth"id":"95907bc09-568976456-c6a5a-4f87g"[]}6778:---


I have managed to get as far as:
(.*?)(\"id\":\"[^"]*)+

which DOES capture the IDs I'm interested in - unfortunately also the ones I don't need (before
AAAAA
and after
ZZZZZ
).

This and this and this comes close - but still no cigar.
Any help would be greatly appreciated - either a pointer in the right direction or a complete working regex (even though a working example would be preferred :-) )

Thanks regex gurus !

Answer

You can first capture everything between these two keys words with (?<=AAAAA).*?(?=ZZZZZ), then search in the result for this regex (?<=\"id\":\").*?(?=\"). The latter will match everything between "id":" and " excluding those.