繁体   English   中英

使用Regex.Match拆分

[英]Splitting with Regex.Match

我有一个类似“颜色:黄色,绿色,白色”的字符串。 我需要从中获取一个数组(“黄色”,“绿色”,“白色”),并且需要使用一个正则表达式来完成。

我正在尝试应用类似

var result = Regex.Match("Colors: green, white, yellow", @":(\s(?<result>.*?)(,|$))*");

我得到的是那个result.Groups [“ result”] ==“ yellow”

如何获得所有其他颜色? 可能还有另一种方法吗?

此代码段将从result Match对象为您提供一系列颜色。

string[] colours = result.Groups["result"].Captures
    .Cast<Capture>()
    .Select(c => c.Value)
    .ToArray();

尝试result.Groups [“ result”]。Captures

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM