user1514499
user1514499

Reputation: 771

regex - find the value between quotes for a particular key

From a string, I have to find the 'value' for a particular key(variable)
to find a regex pattern to find the value between the quotes.

keyx:'value1',keyn:'value2', keys....

like for keyn - the regex must return value2

It is not a json format. This string is extracted from an XML tag.

I have tried this regex (["'])(?:(?=(\?))\2.)*?\1 and trying to modify it to make it work for my scenario

Upvotes: 0

Views: 52

Answers (1)

Poul Bak
Poul Bak

Reputation: 10929

You can use the following regex to your values:

key\w:'(.*?)'[, ]*?

You need to set the global flag.

The regex starts by mathing 'key' followed by a Word char, colon and a single quote, then creates a capture Group of everything between the single quotes, followed by a single quote, and zero or more commas and Spaces.

You get your value from Group 1. That will produce 'value1' and 'value2'.

Upvotes: 1

Related Questions