David Torrey
David Torrey

Reputation: 1342

REGEX differentiating inner match from outer match

I am working with REGEX on complex JSON representing objects, each represented by UUID's. The problem is the REGEX that matches each individual object also matches a larger pattern. Take, for example, the following:

{_id:"(UUID)" value:"x"}(additional info here),{_id:"(UUID)" value:"y"}(additional info here)

now if I do a pattern such as /{_id:"(.+?)".+value:"(.+?)"}/g to grab the ID and Value of each, instead of matching each one individually will it not match the larger pattern, that being the first id and the last value?

What's the best way to ensure each group is individually pulled and not a larger pattern which also matches?

Upvotes: 1

Views: 72

Answers (2)

David Torrey
David Torrey

Reputation: 1342

I was able to figure it out, I wasn't using the non-greedy "?" correctly. I was able to get each one individually by using the following:

/{_id:"(.+?)".+?value:"(.+?)"}/g

Upvotes: 1

m0meni
m0meni

Reputation: 16435

The problem with you regex /{_id:"(.+?)".+value:"(.+?)"}/g

was that .+ should be .+?

So now the regex is:

{_id:"(.+?)".+?value:"(.+?)"}/g

https://regex101.com/r/xK0qJ8/2

Upvotes: 1

Related Questions