11

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*\")
bflemi3
  • 6,698
  • 20
  • 88
  • 155
  • Add compete code **[mcve]** – Tushar May 03 '16 at 14:36
  • 6
    You can use [`JSON.parse(string)[key_name]`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) – Washington Guedes May 03 '16 at 14:36
  • You can use ("(.*?)") but I doubt if that is what you really need/want –  May 03 '16 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. – MichaelK May 03 '16 at 14:44
  • @user6188402 Yes, that could be a possibility as well. I've updated my question to include escaped quotes. – bflemi3 May 03 '16 at 14:47
  • With or without escaped quotes, you can parse the string to get the object and then access the key's value – Washington Guedes May 03 '16 at 14:48
  • @MichaelKarnerfors That will match the keys, not just the values. That's why he has the colon. – Barmar May 03 '16 at 14:48
  • @user6188402 How do you parse a string in an IDE's find/replace dialogue? – Barmar May 03 '16 at 14:49
  • @Barmar. I copy the text to my browser console ;) – Washington Guedes May 03 '16 at 14:50
  • @user6188402 that's a good idea. – bflemi3 May 03 '16 at 14:51
  • @Barmar That is why I instead added a **comma**. ;) – MichaelK May 03 '16 at 14:52
  • @MichaelKarnerfors So now it will match from the beginning of the key to the end of the value. – Barmar May 03 '16 at 14:53
  • See http://stackoverflow.com/questions/249791/regex-for-quoted-string-with-escaping-quotes for how to match a quoted string including escaped quotes inside it. – Barmar May 03 '16 at 14:53
  • @Barmar: so add a lazy directive then. I cannot do that at the moment as I am now on a mobile device and in a hurry. :) – MichaelK May 03 '16 at 15:02
  • @MichaelKarnerfors Laziness won't solve the problem. That will cause the regexp to stop as soon as a match is found, but doesn't make it start later than necessary. That's why he needs to skip over the `:` before starting. – Barmar May 03 '16 at 15:08
  • @Barmar A match will not be found on the key because there is a **comma** at the end of the regex. So the regex will match the value and the comma, with the first replacement group being the value (quotes included). – MichaelK May 03 '16 at 15:12
  • @MichaelKarnerfors There's no comma after the last value, so it won't match that one. – Barmar May 03 '16 at 15:13
  • @MichaelKarnerfors When you get to a real browser, I suggest you try your idea at regex101.com. – Barmar May 03 '16 at 15:14
  • @Barmar That last value he can replace manually. And **you** can be a smidgeon helpful, see the spirit of what I am trying to do and post a viable solution in stead of laying effort on going "that won't work" over and over. :-/ – MichaelK May 03 '16 at 15:32
  • @MichaelKarnerfors Someone already posted an answer, and I posted a link to a related question. Why didn't you post your solution as an answer if you think it's right? – Barmar May 03 '16 at 15:42

6 Answers6

12

You can use the pattern:

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

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

demartis
  • 309
  • 2
  • 5
7

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

Redu
  • 25,060
  • 6
  • 56
  • 76
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

Jesse de gans
  • 1,432
  • 1
  • 14
  • 27
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+)
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

Akumaburn
  • 530
  • 1
  • 5
  • 17
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.