0

I'm having an issue capturing a repeating group in regex, I have following text in log file and I want to add "\" (backslash) to '"'(semicolon) values so the Json becomes valid

So far I am able to extract the following value with regx "key1":{(.),(.)}

"key1":{ "v1":"M.V. "1002 xyz" V.089E", "v2":"M.V. "tr34 abc" V.089E" }

Which gives me 2 splits of

"v1":"M.V. "1002 xyz" V.089E"

"v2":"M.V. "tr34 abc" V.089E"

But since I do not know howmay key-value pairs will be inside key1's value I cannot use "key1":{(.),(.)} as there could potentially be a v3, v4 etc How can I make use of repeating groups to get all the separate splits for keys inside the Json Thanks you

SD13
  • 119
  • 1
  • 12
  • Definitely not about parsing Json, Its about adding escape character inside the string, the string so happened to be Json, its regx. I know the Json is not valid. – SD13 Apr 26 '18 at 12:33
  • (Most) Regex engines do not support repeating capture groups (why? search on google). Instead, the desired approach here would be splitting. So, first, take the value between `{ }` using Regex, then simply split by coma. You will end up with all v1, v2, v3 and so on within a list, and each vX will have it's own index. Then, you can work on each separate vX key-value pairs. – Asunez Apr 26 '18 at 13:01
  • @WiktorStribiżew This is not a valid JSON, so parsing it with any JSON library will be hard - hence the OP is using Regex for this. Can you unlock this question? – Asunez Apr 26 '18 at 13:03
  • @Asunez OP wrote: *How can I make use of repeating groups to get all the separate splits for keys inside the **Json***. It seems OP just didn't post all the data. – Wiktor Stribiżew Apr 26 '18 at 13:16
  • @Asunez thank you for you reply on splitting.I will try it our the Splitting. – SD13 Apr 26 '18 at 15:29
  • @WiktorStribiżew , Sorry I did not understand when you said "It seems OP just didn't post all the data" What amendments you are referring to? – SD13 Apr 26 '18 at 15:34
  • I think you can try use `:` and `:{` to figure out it's a key or string or object. If it's a key add \ before ". If it's a string add \ before " which enclose the value. – 吳約南 Mar 12 '20 at 17:35
  • Here is regex for you. Welcome :)) `"(?:[^,:][^"]+")+` Can help you capture all real enclosing "..content.." even it includes " . Then you just need to add \ before the start " and end " . I never code in C# regex: `/"(?:[^,:][^"]+")+/g` – 吳約南 Mar 12 '20 at 17:46

0 Answers0