Hiro Protagonist Hiro Protagonist - 1 year ago 61
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

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:


I have managed to get as far as:

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

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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download