0

I have a Json string that I need to extract some data from using Regex in C# The string is something like this:

{
  "key1": "value1",
  "key2": "value2",
  "key3": "value3",
  "key4": [
    {
      "arrayKey1": 1,
      "arrayKey2": "something",
      "arrayKey3": "somethingelse"
    },
   {
      "arrayKey1": 2,
      "arrayKey2": "something2",
      "arrayKey3": "somethingelse2"
    },
   {
      "arrayKey1": 3,
      "arrayKey2": "something3",
      "arrayKey3": "somethingelse3"
    }
  ],
  "some very long text here": ""
  "anotherKey": "value",
  "keylast": "valuelast"
}

I want to extract the array's values with named groups, I did it with the following regex: (?:"arrayKey1": (?<arrayKey1>[^"]+),[\n\t ]+"arrayKey2": "(?<arrayKey2>[^"]+)",[\n\t ]+"arrayKey3": "(?<arrayKey3>[^"]+)")

This works great and I get each match for each item of the array with 3 groups of each key.

Now I want to add an extra match that will contain only the value of "anotherKey" I can't get to work, Here are some regexs I have tried but didn't work:

(?:"arrayKey1": (?<arrayKey1>[^"]+),[\n\t ]+"arrayKey2": "(?<arrayKey2>[^"]+)",[\n\t ]+"arrayKey3": "(?<arrayKey3>[^"]+)")(?:[\s\S]*)(?:"anotherKey": "(?<anotherKey>[^"]+)")

This one does get the "anotherKey", but it only returns the first item in the array, not all of them.

Also: https://regex101.com/r/mfXlRs/1

Can someone put me in the right way?

Thanks

Ayoub_B
  • 702
  • 7
  • 19
  • 5
    *I have a Json string that I need to extract some data from using Regex in C#* - Actually, you need a JSON parser. Like [JSON.NET](https://www.newtonsoft.com/json). See [this post](https://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c) with some hints on extracting data from JSON strings. – Wiktor Stribiżew Jul 04 '18 at 11:18
  • Thank you for the reply, I'm aware of that, but the problem is I can't use a JSON parser in my situation! – Ayoub_B Jul 04 '18 at 12:09
  • Sorry, that is not right. In *every* situation when you have a JSON string, use a JSON parser. Always. Unless it is invalid JSON. Again, even then, you should *fix* the JSON and still use a JSON parser. – Wiktor Stribiżew Jul 04 '18 at 12:23

1 Answers1

0

Your regex for arrayKey1..3 (only) gave three separate matches, and in each match you got the 3 required values.

Now, after you added the fragment looking for anotherKey, but at the parent level, the situation changed. Now you have only a single match, because:

  • Your "old" regex matches only the first set of arraykeys.
  • Then (?:[\s\S]*) matches everything up to anotherKey, including both remaining sets of arraykeys.
  • The added part matches just anotherKey.

Maybe you should perform your matching in 2 separate steps:

  • Start from the first (old) match, getting 3 matches for arraykeys and store them somewhere.
  • Then run the second match, only for anotherKey.

Adding + quantifier to the "old" group will not help, because if a capturing group was matched several times, then the group would hold only the last match.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41