我有一種方法,只是簡單地檢查一個文本文件的幾個字符串 - 儘管幾個字符串不被拾取爲匹配,即使它們出現在文本文檔中。我已經包括有問題的字符串,這是沒有在這個例子中發現:正則表達式匹配方法無法正常工作
static void Main(string[] args)
{
string str = @"1009 10.32.0.28 03/05/2012 09:11:48 The license expires in 1192 day(s).";
StreamReader sr = new StreamReader(@"event_history.txt");
string allRead = sr.ReadToEnd();
sr.Close();
string regMatch = str;
if (Regex.IsMatch(allRead, regMatch))
{
Console.WriteLine("found\n");
}
else
{
Console.WriteLine("not found\n");
}
Console.ReadKey();
}
event_history.txt
1009 10.32.0.28 03/05/2012 09:11:48 The license expires in 1192 day(s).
如果我更換正則表達式匹配說「測試」,然後添加「測試「對於文本文件,它將其視爲匹配沒有問題:S
您正在使用正則表達式以錯誤的方式。爲什麼不使用string.Contains? –
如果使用REGEX,很多標點符號必須被轉義。如果你確切地知道你在找什麼,我們包含(),即。說過。否則,請參閱http://msdn.microsoft.com/en-us/library/az24scfc.aspx以獲取一些解釋 –
在使用它們之前,您可能首先需要真正學習正則表達式。他們是一種僞裝的語言,讓你的生活變得更有趣。如果你陷入了有趣的受虐狂概念,那就是;) –