2010-09-13 146 views
2

我已經嘗試了很多東西,但我無法弄清楚如何去下面的字符串匹配正則表達式問題

這是我的例子。 Btw使用2.0緊湊框架,如果它很重要。

string received = "AT+CMEE=1\r\r\nOK\r\n"; 

Regex regex = new Regex(received , RegexOptions.Multiline); 

// I have tried many things 
Match match1 = regex.Match(".*AT\+CMEE=1\r\r\nOK\r\n.*"); 
Match match2 = regex.Match(".*AT\\+CMEE=1\r\r\nOK\r\n.*"); 
Match match3 = regex.Match(".*OK.*"); // this one completely confuses me. 

我在做什麼錯?請幫忙。

預先感謝您。

+3

我覺得你困惑的模式和字符串匹配。 – 2010-09-13 10:14:01

+0

非常感謝。 – Chauncat 2010-09-13 10:39:49

回答

0

您也需要轉義特殊字符,如+和\像這樣:

"AT\+CMEE=1\\r\\r\\nOK\\r\\n" 

,或者你可以前綴您字符串@使其文字:

@"AT+CMEE=1\r\r\nOK\r\n" 

嘗試測試在http://regexlib.com/RETester.aspx

1

Expresso可以幫助您測試正則表達式併爲您生成C#或VB.NET代碼。在這種情況下,它會逃脫你的字符串。

http://www.ultrapico.com/Expresso.htm

附:我不屬於Ultrapico,我只是使用Expresso來不時地構建和測試正則表達式。

+0

感謝這是我一直使用的東西。偉大的工具 – Chauncat 2011-01-20 22:09:25