tags:

views:

126

answers:

2

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.

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.

A: 

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

TimS
A: 

Expresso can help you test your regular expressions and generate the C# or VB.NET code for you. In this case it would have escaped the string for you.

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

P.S. I am not affiliated to Ultrapico, I just use Expresso to build and test regular expressions from time to time.

paludarium