2010-09-13 298 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