C# 实现文件文本的正则表达式查询
1 static void QueryWithRegEx()
2 {
3 string startFolder = @"c:\program files\Microsoft Visual Studio 9.0\";
4 IEnumerable<FileInfo> fileList = GetFiles(startFolder);
5
6 Regex searchTerm = new Regex(@"Visual (Basic|C#|C\+\+|J#|SourceSafe|Studio)");
7
8 var queryMatchingFiles = from file in fileList
9 where file.Extension == ".htm"
10 let fileText = System.IO.File.ReadAllText(file.FullName)
11 let matches = searchTerm.Matches(fileText)
12 where matches.Count > 0
13 select new
14 {
15 name = file.FullName,
16 matchedValues = from System.Text.RegularExpressions.Match match in matches
17 select match.Value
18 };
19 }
2 {
3 string startFolder = @"c:\program files\Microsoft Visual Studio 9.0\";
4 IEnumerable<FileInfo> fileList = GetFiles(startFolder);
5
6 Regex searchTerm = new Regex(@"Visual (Basic|C#|C\+\+|J#|SourceSafe|Studio)");
7
8 var queryMatchingFiles = from file in fileList
9 where file.Extension == ".htm"
10 let fileText = System.IO.File.ReadAllText(file.FullName)
11 let matches = searchTerm.Matches(fileText)
12 where matches.Count > 0
13 select new
14 {
15 name = file.FullName,
16 matchedValues = from System.Text.RegularExpressions.Match match in matches
17 select match.Value
18 };
19 }