0

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

6
  • How about: (?<=keyx:')[^']+(?=') ? ... assuming that the values would always be surrounded by single quotes. Commented Oct 23, 2018 at 14:16
  • What language are you using this in? If you split string by "," , then you could use something '.+' to match on each of the split bits Commented Oct 23, 2018 at 14:17
  • @Rashmirathi What if some of the values have commas inside them? Commented Oct 23, 2018 at 14:18
  • @Rashmirathi java Commented Oct 23, 2018 at 14:33
  • Possible duplicate of Regex to get string between curly braces "{I want what's between the curly braces}" Commented Oct 23, 2018 at 14:36

1 Answer 1

1

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'.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.