C#中利用正则表达式获取字符串中双引号包含的内容

代码:

using System.Text.RegularExpressions;

namespace GetData
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var lines = File.ReadAllLines("data.txt");
            Regex re = new Regex("(?<=\").*?(?=\")", RegexOptions.None);

            var myList = new List<string>();
            foreach (var line in lines)
            {
                MatchCollection mc = re.Matches(line);
                foreach (Match m in mc)
                {
                    myList.Add(m.Value);
                    Console.WriteLine(m.Value);
                }
            }

            File.WriteAllLines("result.txt", myList.ToArray());
            Console.WriteLine("---------------------");
            Console.ReadKey();
        }
    }
}

 

posted @ 2022-11-24 11:44  wzwyc  阅读(436)  评论(0编辑  收藏  举报