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.

1
  • Apologies! Those comma's should not have been in there - I've removed them from the question now. Thanks for highlighting that. Oct 3, 2011 at 12:31

1 Answer 1

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();

Not the answer you're looking for? Browse other questions tagged or ask your own question.