1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Text.RegularExpressions; 6 using System.Threading.Tasks; 7 8 namespace 正则匹配头文件 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 string str = "\n #include \"cms.h\" \n void "; 15 string pattern = @"#\s*include\s*(""|<)(?<headname>\w+\.(h|H)\b)(""|>)"; 16 MatchCollection mc = Regex.Matches(str, pattern); 17 foreach (Match match in mc) 18 { 19 GroupCollection gc = match.Groups; 20 Console.WriteLine(gc["headname"].Value); 21 //Console.WriteLine(match.ToString()); 22 } 23 24 Console.WriteLine(); 25 Console.ReadKey(); 26 } 27 } 28 }