Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am developing a application in c# using gsm modem

when i send a message using AT command, it returns a confirmation messsage

"REL LOCK OPEN\r\n+CMGS: 67\r\n\r\nOK\r\n"

so how can i match the above with regular expression

i am trying this

Regex r = new Regex(@"\r\n(.+)\r\n\+CMGS: (\d+)\r\n\r\n(.+)\r\n");

but it's not working
Posted
Updated 27-Nov-13 21:33pm
v5
Comments
Ron Beyer 27-Nov-13 23:53pm    
"it's Very urgent"... not to me...

It would help us if you told us what parts you are trying to match, what are the important parts of that string? What parts change?
NABIN SEN 28-Nov-13 0:08am    
CMGS: 67
Sergey Alexandrovich Kryukov 28-Nov-13 0:19am    
The questions like "what is the regular expression for the string" cannot make sense, ever. A string does not define any expression. You should formally define what set of string should cause what match and describe required match (or list of matched, or replacement result)...
—SA

1 solution

Firstly, ignore the newlines - or better, dispose of them using string.replace, then work on that ('\n' is not necessarily what you think, and Regexes don't work well with them):
C#
string inp = @"REL LOCK OPEN\r\n+CMGS: 67\r\n\r\nOK\r\n";
inp = inp.Replace(@"\r\n", @"$");
Match m = Regex.Match(inp, @"(.+)\$\+CMGS: (\d+)\$\$(.+)\$");
if (m.Success)
    {
    Console.WriteLine(m.Value);
    foreach (Group g in m.Groups)
        {
        Console.WriteLine("   {0}", g.Value);
        }
    }
Will generate:
CSS
REL LOCK OPEN$+CMGS: 67$$OK$
   REL LOCK OPEN$+CMGS: 67$$OK$
   REL LOCK OPEN
   67
   OK

Which you should be able to work with.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900