我想在C#中拆分一个字符串,它看起来像这样:
a : b : "c:d"
因此,结果数组将为
Array[0] = "a"
Array[1] = "b"
Array[2] = "c:d"
我应该使用什么正则表达式才能实现所需的结果。
非常感谢
string example = "a : b : \"c:d\"";
string[] splits = Regex.Split(example, @"\s:\s");
这个正则表达式在我的RegexBuddy中似乎可以工作:
(\w+)\s:\s(\w+)\s:\s"(\w+:\w+)"
输入:
a : b : "c:d"
匹配组:
- a
- b
- c:d
一如既往地要小心并了解正则表达式实际上是做什么的。不要盲目复制。这个表达式匹配单词字符\w
,空格\s
等。请考虑您的输入数据实际上会有什么!