0

This is possible duplicate of many questions. But I have a slightly different scenario here off the many questions and answers I read.

I want to split a string by = but skip the = that may appear in double-quotes. Also, this double-quoted string in itself contains double quotes as well.

 Example
 string example1 = "propery=value";
 string example2 = "property=\"value1=value2\"";
 string example3 = "property= \" \"value1\" = value2 \" ";
 string regex = "[=](?=(?:[^\"]* \"[^\"]*\")*[^\"]*$)"
 string[] ex1 = Regex.Split(example1, regex) // result: [property], [value]

 //Does not work for these cases
 string[] ex2 = Regex.Split(example2, regex) // result: [property="value1=value2"]
 //Expected result: [property], ["value1=value2"]
 string[] ex3 = Regex.Split(example1, regex) // result: [property= \" \"value1\" = value2 \"]
 //Expected Result: [property],["\"value1\"=value2"]

I checked on regexr.com and it seems to identify the correct =.

Result on regexr.com

Zankhana Rana
  • 169
  • 2
  • 16
  • What is the actual problem you are trying to solve? Where are these key-value pairs coming from? – Sam Axe Jun 18 '20 at 20:21
  • maybe test with something that supports .Net like http://regexstorm.net/tester. Not all Regex engines work the same – juharr Jun 18 '20 at 20:23
  • @SamAxe They are coming from a CSV file. One of the columns in the CSV file is given as this key-value pair separated by =. The value can contain double quotes and '='. – Zankhana Rana Jun 18 '20 at 20:23
  • 2
    that seems like a problem easily fixable without regexes. Any reason to use regex? That a simple string split on the first equal. – Pedro Rodrigues Jun 18 '20 at 20:23
  • Before I reach for regex I always remember the wise words of an internet user I've long forgotten the name of: "You had a problem and tried to solve it using regex. Now you have two problems." While splitting is tempting you are probably better off just using string.IndexOf to locate the first `=` and then string.Substring around it. – Sam Axe Jun 18 '20 at 20:23
  • @juharr Thanks. Trying to check on the link you suggested. – Zankhana Rana Jun 18 '20 at 20:24
  • @SamAxe you might want to check [that](https://stackoverflow.com/a/1732454/3343753) – Pedro Rodrigues Jun 18 '20 at 20:24
  • 1
    @PedroRodrigues That sounds reasonable. I was hitting my head for so long trying to fix this not thinking of any other alternative. – Zankhana Rana Jun 18 '20 at 20:26
  • Awesome. Here is a fiddle for you: https://dotnetfiddle.net/631LJX – Pedro Rodrigues Jun 18 '20 at 20:30
  • Whoever closed this made a mistake. – Pedro Rodrigues Jun 18 '20 at 20:31
  • 1
    No regex needed. 1. Replace `"="` with another symbol like a pipe `|`. 2. Split using `=`. 3. Replace pipe back with a `"="`. 4. Have a beer. – CodingYoshi Jun 18 '20 at 20:32
  • `I want to split a string by = but skip the = that may appear in double-quotes. Also, this double-quoted string in itself contains double quotes as well.` this not will happen using splitk, why yuo think split is need ? –  Jun 18 '20 at 20:32
  • the supr dupr is useless. –  Jun 18 '20 at 20:33
  • @Edward I had a simple scenario of split on = for which I was using the split function. things got complicated when " and = showed up in the value and I couldn't think of the simple indexOf solution. – Zankhana Rana Jun 18 '20 at 20:34
  • @PedroRodrigues yes. It does seem duplicate at first but is very little different as well. Thank you for the fiddle. – Zankhana Rana Jun 18 '20 at 20:35
  • then why insist on using _split_ ? use regex split simulation that matches `=` not inside quotes. I can give it to you right now, open up the post and i give it to yuio –  Jun 18 '20 at 20:43
  • @Edward I don't think I can reopen the question. I might have to ask a new one. I think using indexOf should work. – Zankhana Rana Jun 18 '20 at 20:48
  • it a simple regex but not for split, for parse equal separated value same as csv, parse the field and take all quoted data with it. as simple as can be, dumps into an array just fine –  Jun 18 '20 at 21:01

0 Answers0