2012-05-03 110 views
0

我有一種方法,只是簡單地檢查一個文本文件的幾個字符串 - 儘管幾個字符串不被拾取爲匹配,即使它們出現在文本文檔中。我已經包括有問題的字符串,這是沒有在這個例子中發現:正則表達式匹配方法無法正常工作

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

+3

您正在使用正則表達式以錯誤的方式。爲什麼不使用string.Contains? –

+0

如果使用REGEX,很多標點符號必須被轉義。如果你確切地知道你在找什麼,我們包含(),即。說過。否則,請參閱http://msdn.microsoft.com/en-us/library/az24scfc.aspx以獲取一些解釋 –

+1

在使用它們之前,您可能首先需要真正學習正則表達式。他們是一種僞裝的語言,讓你的生活變得更有趣。如果你陷入了有趣的受虐狂概念,那就是;) –

回答

1

regMatch = str和str不是正則表達式。爲什麼使用RegEx?你應該使用類似的東西來

".*The license expires in [0-9]* day(s).". 

採取進一步行動,爲IP 10.23.0.28的所有條目:

"1009 10\.32\.0\.28 ..\/..\/.... ..\:..\:.. The license expires in [0-9]* day(s)." 

使用文件regex.txt例如:

$ cat regex.txt 
1009 10.32.0.28 03/05/2012 09:11:48 The license expires in 1192 day(s). 
1009 10.32.0.28 04/05/2012 09:11:48 The license expires in 1192 day(s). 
1009 10.32.0.29 04/05/2012 09:11:48 The license expires in 1192 day(s). 
1009 10.32.0.30 04/05/2012 09:11:48 The license expires in 1192 day(s). 

結果是:

$ grep "1009 10\.32\.0\.28 ..\/..\/.... ..\:..\:.. The license expires in [0-9]* day(s)." regex.txt 
1009 10.32.0.28 03/05/2012 09:11:48 The license expires in 1192 day(s). 
1009 10.32.0.28 04/05/2012 09:11:48 The license expires in 1192 day(s). 

如果那是你總是想檢查的字符串(1009,1192天,IP地址和日期 總是靜態的)。

用途:

".... 10\.32\.0\.28 04\/05\/2012 ..\:..\:.. The license expires in 1192 day(s)." 
+1

這非常貪婪,它忽略了OP發佈的其他文本,並不關心是否沒有任何數字存在,也不會逃過括號或最後一個'。 '。 – Joe

+0

是的,你必須添加額外的正則表達式來精確定位。 YOu當然可以通過去除前綴中的通配符來進一步處理。 –

0

您使用Regex.IsMatch作爲String.Contains。正則表達式有其自己的語法,因此不會起作用(一般情況下)。

在您的特定示例中,您需要繞過s來躲避缺口,因爲正則表達式引擎現在不會匹配它們,它們是捕獲操作符。點也應該逃脫,雖然諷刺的是正則表達式點匹配點。所以:

string str = @"1009 10\.32\.0\.28 03/05/2012 09:11:48 The license expires in 1192 day\(s\)\."; 
+2

我認爲保留字符串會更容易,並使用'Regex.Escape()'。 – svick

1

str是不是你想要完成的正確的正則表達式。例如.意味着任何字符和s附近的括號都是分組,並且不會被捕獲。你實際上只需要檢查是否allRead包含str如果這是你總是想檢查的字符串(1009,1192天,IP地址和日期總是靜態的)。

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(); 

if(allRead.Contains(str)) 
{ 
    Console.WriteLine("found\n"); 
} 
else 
{ 
    Console.WriteLine("not found\n"); 
} 

如果你正在尋找在這裏捕獲的非靜態值的正則表達式你去

string str = @"\d+ \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} \d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2} The license expires in \d+ day\(s\)\."; 
+1

嗯,這是一個有效的正則表達式,但它不會做OP想要的。 – svick

0

之所以正則表達式不匹配是因爲(和)在正則表達式的特殊字符。它們代表一個分組,這是您稍後要參考的值。要將它們更改爲常規字符,請在它們前面追加\ \右鍵。所以你的正則表達式應該看起來像

@"1009 10.32.0.28 03/05/2012 09:11:48 The license expires in 1192 day\(s\)." 
相關問題