C# RegEx to create string array split on spaces and phrases (in quotes) [duplicate]
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.
The regex:
".*?"|[^\s]+
Usage:
String input = @"this is ""a simple"" test";
String[] matches =
Regex.Matches(input, @""".*?""|[^\s]+").Cast<Match>().Select(m => m.Value).ToArray();
精彩评论