C# 正则匹配c语言项目的头文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace 正则匹配头文件
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "\n #include \"cms.h\" \n void  ";
            string pattern = @"#\s*include\s*(""|<)(?<headname>\w+\.(h|H)\b)(""|>)";
            MatchCollection mc = Regex.Matches(str, pattern);
            foreach (Match match in mc)
            {
                GroupCollection gc = match.Groups;
                Console.WriteLine(gc["headname"].Value);
                //Console.WriteLine(match.ToString());
            }

            Console.WriteLine();
            Console.ReadKey();
        }
    }
}

 

posted @ 2022-07-12 11:23  羽小兮  阅读(108)  评论(0)    收藏  举报