1

Here's an example string -

"EP(DebugFlag="N",UILogFlag="N")" Other words here

I'd like to split the string by spaces, but need to keep quoted phrases together - even if there are quotes within quotes. So I'd like the sample string to be split as -

  1. "EP(DebugFlag="N",UILogFlag="N")"
  2. Other
  3. words
  4. here

I'm not sure how to take the quotes into consideration (finding the starting and ending one). Is there an easy way to do this?

Thanks!

Greg
  • 77
  • 1
  • 8
  • Why do you want to use a regular expression for that? Nesting/stacking regular expressions are a maintenance hell. A simple parser would take a couple of lines of code and be written faster than this question. – CodeCaster May 30 '17 at 19:28
  • I'd be open to any solution - thanks! – Greg May 30 '17 at 19:30
  • There's an ambiguity. that is probably impossible to solve given your format: Is this a single item : `"EP(DebugFlag="N",UILogFlag="N")"` or is it 5 items : `"EP(DebugFlag=" N ",UILogFlag=" N ")"` ? – spender May 30 '17 at 19:38
  • It's supposed to be a single item. Thanks CodeCaster - I will take a look at the sample – Greg May 30 '17 at 19:40
  • @spender there are no spaces there. I guess there would be ambiguity if the quotes were unbalanced. – CodeCaster May 30 '17 at 19:42
  • @Greg My point is that without more info about how to disambiguate whether a quote is a closing or an opening quote, I believe that there will frequently be more than one solution when splitting. – spender May 30 '17 at 19:44
  • @CodeCaster or if a quoted item with leading/trailing space enters the fray. – spender May 30 '17 at 19:44
  • I see what you're saying now spender. What I'm trying to do is write a google like search on a database and I need some way to determine each of the separate terms and phrases. Some of the phrases may have embedded quotes and I'm having trouble parsing everything out. – Greg May 30 '17 at 19:46
  • You will find solutions at https://stackoverflow.com/q/1757065/873282 – koppor May 15 '23 at 07:20

1 Answers1

0

You mean something lie this:

string example = @"EP(DebugFlag='N',UILogFlag='N') Other words here";

var result = example.Split(new string[]{" "}, StringSplitOptions.RemoveEmptyEntries).ToList();

foreach(var phrase in result)
{
    Console.WriteLine("{0}", phrase);
}

Note: as @CodeCaster suggested, i have to mention that i replaced double-quote with single quote to provide "working example". If your sample differs to mine, you have to provide exact text without arounding quotes.

Maciej Los
  • 8,468
  • 1
  • 20
  • 35
  • You conveniently parsed the input string as a human and replaced the troublesome double quotes with single quotes. If that were an option, the OP wouldn't have to ask this question. You may want to mention what you did to the string in your answer. – CodeCaster May 30 '17 at 19:40
  • Yes! Thanks so much – Greg May 30 '17 at 19:40
  • @CodeCaster. You're right. I should mention this at begin of my answer. I'll take it in the future. – Maciej Los May 30 '17 at 19:45
  • 1
    Oh I missed what you did entirely. The string the OP shows is not a string in C# syntax, the quotes are actually there. – CodeCaster May 30 '17 at 20:01