简体   繁体   English

正则表达式问题

[英]Regular Expression Question

I have tried many things but I can not figure out how go get a match on the following string 我已经尝试了很多事情,但是我不知道如何在以下字符串上进行匹配

Here is my example. 这是我的例子。 Btw using 2.0 compact framework if it matters. 顺便说一句,如果有关系,请使用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.

What am I doing wrong? 我究竟做错了什么? Please help. 请帮忙。

Thank you in advance. 先感谢您。

Expresso can help you test your regular expressions and generate the C# or VB.NET code for you. Expresso可以帮助您测试正则表达式并为您生成C#或VB.NET代码。 In this case it would have escaped the string for you. 在这种情况下,它将为您转义字符串。

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

PS I am not affiliated to Ultrapico, I just use Expresso to build and test regular expressions from time to time. PS我不隶属于Ultrapico,我只是不时使用Expresso构建和测试正则表达式。

You either need to escape special characters such as + and \\ like so: 您要么需要转义特殊字符(例如+和\\),如下所示:

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

or you can prefix you string with @ to make it literal: 或者,您可以在字符串前面加上@使其字面值:

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

Try testing at http://regexlib.com/RETester.aspx 尝试通过http://regexlib.com/RETester.aspx进行测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM