简体   繁体   中英

Splitting with Regex.Match

I've a string like "Colors: yellow, green, white". I need to get an array ("yellow", "green", "white") from it and it needs to be done with one regex.

I'm trying to apply something like

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

what I get is that result.Groups["result"]=="yellow"

How can I get all the other colors? May be there's another way to do this?

This snippet will get you an array of colours from your result Match object.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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