13

Given the following key value pairs, how could I match just the values (including the quotes)?

Explanation: I'm doing a find and replace in my IDE. I have hundreds of key/value pairs where the values need to be changed from strings to objects. So basically replacing the value.

"ElevationFilenameIn": "Input raster elevation file",
"TargetCRS": "Target vertical coordinate reference system Type",
"featureName": "The name of the feature to extract, for example \"Vegetation\" or \"Water\"",
"TargetCRScode": "Target vertical coordinate system Code",
"TargetCRSfile": "The projection (.prj) file in shoebox to be used for this inputfile"

My attempt (which is not working, not even close):

[:]\s*(\"\w*\")
20
  • Add compete code minimal reproducible example Commented May 3, 2016 at 14:36
  • 6
    You can use JSON.parse(string)[key_name] Commented May 3, 2016 at 14:36
  • You can use ("(.*?)") but I doubt if that is what you really need/want Commented May 3, 2016 at 14:43
  • No need to bracket the colon, or even include it in the regex. This should do it... (\".*\"), Do not forget to add that trailing comma in your replacement. Commented May 3, 2016 at 14:44
  • @user6188402 Yes, that could be a possibility as well. I've updated my question to include escaped quotes. Commented May 3, 2016 at 14:47

6 Answers 6

13

You can use the pattern:

[:]\s(\".*\")

and test it following this link: https://regex101.com/r/nE5eV3/1

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

4 Comments

what if it was a object e.g. "ElevationFilenameIn": { "foo": "bar" }
@EdwinIkechukwu, do you need to match the entire string { "foo": "bar" } including braces?
How to get a single value based on the key?
This fails if multiple key value pairs present in same line eg. regex101.com/r/AgpwcS/1
8

I guess this one does the job also well. One good part it doesn't use any capture groups one bad part it's more costly compared to the accepted answer.

[^:]+(?=,|$)

Regular expression visualization

Debuggex Demo

Regex101 Demo

1 Comment

and [^:\"]+(?=,|$) to get rid of the quotes :)
2

Get value

[^:"]+(?="})

enter image description here

Get value by key

If you wish to select a specific key that can be done like so:

[^:KEY"]+(?="})

enter image description here

1 Comment

Is this working?
2

All key value par in complex JSON object.

"[a-zA-Z0-9 -]*"(?=:):[a-zA-Z0-9 "-ć]*(?<=")|"[a-zA-Z0-9 -]*"(?=:):[a-zA-Z0-9 "-ć]*(?=,)|"[a-zA-Z0-9 -]*"(?=:):[a-zA-Z0-9 "-ć]*(?=\w+)

Comments

1

To get a value by itself without capturing the key:

(?:\"keyname)(?:\"\s?:\s?\")(.*)(?:\")

For example given:

{"keyname":"value"}

This will capture

value

Test here: https://regex101.com/r/Bm1mmK/1

Comments

0

Here's one that works when the value is another json object:

:\s*["{](.*)["}]\s*[,}]

It does not include the quotes/brackets in the capture group. If you want to include those, the capture group is easily modified:

:\s*(["{].*["}])\s*[,}]

The expressions also handle varying whitespace since json ignores whitespace.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.