普通防爆pression分裂的空间,除非报价 [英] Regular Expression to split on spaces unless in quotes
本文介绍了普通防爆pression分裂的空间,除非报价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用.NET Regex.Split方法来此输入字符串分割成一个数组。 它必须分裂空格,除非它被封装在一个引号。
I would like to use the .Net Regex.Split method to split this input string into an array. It must split on whitespace unless it is enclosed in a quote.
输入: 这是我的字符串    它有六  匹配
Input: Here is "my string" it has "six matches"
期望的输出:
- 在这里
- 是
- 在我的字符串
- 这
- 有
- 六类  匹配
我需要什么样的模式?另外,我需要指定任何RegexOptions?
What pattern do I need? Also do I need to specify any RegexOptions?
推荐答案
没有所需的选项
正则表达式:
\w+|"[\w\s]*"
C#:
Regex regex = new Regex(@"\w+|""[\w\s]*""");
或如果你需要排除字:
Regex
.Matches(input, @"(?<match>\w+)|\""(?<match>[\w\s]*)""")
.Cast<Match>()
.Select(m => m.Groups["match"].Value)
.ToList()
.ForEach(s => Console.WriteLine(s));
这篇关于普通防爆pression分裂的空间,除非报价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文