簡體   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