Extract comma separated portion of string with a RegEx in C#

后端 未结 4 1363
失恋的感觉
失恋的感觉 2021-01-22 17:22

Sample data: !!Part|123456,ABCDEF,ABC132!!

The comma delimited list can be any number of any combination of alphas and numbers

I want a regex to match the entri

4条回答
  •  孤街浪徒
    2021-01-22 18:24

    You can either use split:

    string csv = tag.Substring(7, tag.Length - 9);
    string[] values = csv.Split(new char[] { ',' });
    

    Or a regex:

    Regex csvRegex = new Regex(@"!!Part\|(?:(?\w+),?)+!!");
    List valuesRegex = new List();
    foreach (Capture capture in csvRegex.Match(tag).Groups["value"].Captures)
    {
        valuesRegex.Add(capture.Value);
    }
    

提交回复
热议问题