0

Possible Duplicate:
Regular Expression to split on spaces unless in quotes

I am dealing with various strings that I need to split into an array wherever there is a space, except for if that space exists within "quotes".

So for example, I would like this:

this is "a simple" test

..to become:

[0] = this
[1] = is
[2] = "a simple"
[3] = test

Note I would like to retain the quotes surrounding the phrase, not remove them.

Community
  • 1
  • 1
marcusstarnes
  • 6,393
  • 14
  • 65
  • 112
  • Apologies! Those comma's should not have been in there - I've removed them from the question now. Thanks for highlighting that. – marcusstarnes Oct 03 '11 at 12:31

1 Answers1

0

The regex:

".*?"|[^\s]+

Usage:

String input = @"this is ""a simple"" test";
String[] matches =
    Regex.Matches(input, @""".*?""|[^\s]+").Cast<Match>().Select(m => m.Value).ToArray();
Loki Kriasus
  • 1,282
  • 10
  • 22