Convert result of matches from regex into list of string

后端 未结 6 882
余生分开走
余生分开走 2020-12-14 15:22

How can I convert the list of match result from regex into List? I have this function but it always generate an exception,

6条回答
  •  抹茶落季
    2020-12-14 15:43

    A possible solution using Linq:

    using System.Linq;
    using System.Text.RegularExpressions;
    
    static class Program {
        static void Main(string[] aargs) {
            string value = "I have a dog and a cat.";
            Regex regex = new Regex("dog|cat");
            var matchesList = (from Match m in regex.Matches(value) select m.Value).ToList();
        }
    }
    

提交回复
热议问题