开发者

C# RegEx to create string array split on spaces and phrases (in quotes) [duplicate]

开发者 https://www.devze.com 2023-04-10 12:11 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Regular Expression to split on spaces unless 开发者_开发问答in quotes
This question already has answers here: Closed 11 years ago.

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

精彩评论

暂无评论...
验证码 换一张
取 消