-1

I found some regex to extract key-value style format

but recent I encounter some format like below

"Key1"=123,"Key2"="abc","key3"={"subkey1"=12,"subkey2"="cd"},"key4"=456

this what I wish to output

match Key value
match0 Key1 123
match1 Key2 "abc"
match2 key3 {"subkey1"=12,"subkey2"="cd"}
match3 key4 456

how can I escape the "," inside {}

I tried

"(?<key>\w+)"=(?<value>{?"?[^,]*"?}?)

but the result is like

match Key value
match0 Key1 123
match1 Key2 "abc"
match2 key3 {"subkey1"=12
match3 subkey2 "cd"}
match4 key4 456

is there any help to escape only when "," inside {}?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • 1
    There are plenty of questions discussing how to parse arbitrary nested structures with regex... including [the most famous one](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/). Any particular reason you expect that your nested structure is any easier? Please [edit] the question to clarify what exactly makes your case suitable for regex parsing. – Alexei Levenkov Sep 08 '21 at 05:03
  • 1
    my personal guidelines for regex: ***don't*** use them - unless a) the implementation is trivial or b) you absolutely _have_ to. i doubt you'll find a straightforward and maintainable solution with regexes for what you're trying to achieve. try building a parser instead. and maybe talk to whoever created the output if they can give you some pointers - or even a ready made method. after all, it basically looks like JSON, except with `=` replacing `:` (and missing surrounding brackets) – Franz Gleichmann Sep 08 '21 at 05:23
  • thank both recommend, yes, maybe should try to build a parser instead use regex. – Lovetakingphoto Sep 08 '21 at 05:32
  • @Lovetakingphoto note that if you want to implement json approach - you need to consider possibility of `=` present inside string value, though writing simple parser which will substitute all `=` not inside quotes should be easier then writing fully fledged one. – Guru Stron Sep 08 '21 at 06:02

3 Answers3

0

On the sample data "(?<key>\w+)"=(?<value>({.*?})|"?[^,]*"?) seems to work fine - alternating between value in curly braces and the pattern you currently have, though it will work as long as you have only one level of nesting.

Also based on provided data I would consider approach with turning the string into json by replacing = with : (unless some values can have = - then it would be harder, though writing simple parser which will substitute all = not inside quotes should be easier then writing fully fledged one) and wrapping everything in curly braces and the deserializing the result.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
0

You may try this:

,(?=[^}]*(?:,|$))

This will not consider , inside curly brackets

Demo

Source ( run here ) :

  string pattern = @",(?=[^}]*(?:,|$))";
    string input = @"""Key1""=123,""Key2""=""abc"",""key3""={""subkey1""=12,""subkey2""=""cd""},""key4""=456";

  string[] result = Regex.Split(input, pattern, 
                                RegexOptions.IgnoreCase,
                                TimeSpan.FromMilliseconds(500));
  for (int ctr = 0; ctr < result.Length; ctr++) 
  {
     Console.WriteLine("'{0}'", result[ctr]);

  }
Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43
0

You input data appears to be almost valid JSON.

This worked for me:

string source = @"""Key1""=123,""Key2""=""abc"",""key3""={""subkey1""=12,""subkey2""=""cd""},""key4""=456";

string json = $"{{{source.Replace("=", ":")}}}";
// {"Key1":123,"Key2":"abc","key3":{"subkey1":12,"subkey2":"cd"},"key4":456}

var result = Newtonsoft.Json.JsonConvert.DeserializeObject(json);

That gives me:

result

Enigmativity
  • 113,464
  • 11
  • 89
  • 172