2017-02-24 48 views
1

我用下面的正则表达式不同的比赛:(PatientID).*?[\d]+正则表达式返回基于什么是执行表达式

如果我使用记事本+ +的‘在文件中查找’功能执行上的特定文件夹的表情,我得到了1118场比赛231个文件。

enter image description here

如果我使用C#遍历同一个文件夹,并得到所有的文件,我得到223个文件1070场比赛。

static string uat04Dir = @"C:\Users\me\Desktop\UAT04_Generated_Scripts\"; 

static void Main(string[] args) 
{ 
    Regex r = new Regex(@"(PatientID).*?[\d]+"); 
    int matchCounter = 0; 
    int fileCounter = 0; 

    string[] files = Directory.GetFiles(uat04Dir, "*.sql", SearchOption.AllDirectories); 

    foreach (string file in files) 
    { 
     string lines = File.ReadAllText(file); 

     MatchCollection matches = r.Matches(lines, 0); 
     matchCounter += matches.Count; 

     if (matches.Count > 0) 
      fileCounter++; 
    } 

    Console.WriteLine(String.Format("{0} Matches in {1} files.", matchCounter, fileCounter)); 
} 

为什么这会返回不同的结果?

+2

在Notepad ++中,您正在使用不区分大小写的搜索。在C#中,搜索区分大小写。尝试用'RegexOptions.IgnoreCase'标志编译正则表达式,看看你是否得到相同的结果。 –

+0

应该如何:例如http://imgur.com/a/gpVDA vs http://imgur.com/a/SBwvz – sln

回答

1

在记事本++中,您正在使用不区分大小写的搜索作为匹配项复选框未启用。

在C#中,搜索区分大小写,您没有使用任何选项。

用C#代码中的RegexOptions.IgnoreCase标志编译正则表达式,它会得到相同的结果。

+0

其中一个早晨:)谢谢 – sab669