Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello, can you share my the code on how to get only the certain string?
this is the whole string.
AT+CMGL=4\r\r\n+CMGL: 1,2,,48\r\n079136190800101011000BA19084537777F70008AA2200540065007300740020004D0065007300730061006700650020005000440055002E\r\n+CMGL: 2,1,,34\r\n0791361908005043040C9136493875777700005101519103652311D4F29C0E6A96E7F3F0B90C8212AB2E\r\n\r\nOK\r\n

now I wan to parse and get only this.
079136190800101011000BA19084537777F70008AA2200540065007300740020004D0065007300730061006700650020005000440055002E

or
0791361908005043040C9136493875777700005101519103652311D4F29C0E6A96E7F3F0B90C8212AB2E

this is the code that I've used but not working.
C#
public ShortMessageCollection ParseMessagesPDU(string input)
{
    ShortMessageCollection messages = new ShortMessageCollection();

    try
    {
        Regex r = new Regex(@"\+CMGL: (\d+),(\d+),,(\d+)\\r\\n(.*?)\\r\\n", RegexOptions.IgnoreCase | RegexOptions.Singleline);

        Match m = r.Match(input);
        while (m.Success)
        {
            ShortMessage msg = new ShortMessage();
            //msg.Index = int.Parse(m.Groups[1].Value);
            msg.Index = m.Groups[1].Value;
            msg.Status = m.Groups[2].Value;
            msg.Sender = m.Groups[3].Value;
            msg.Alphabet = m.Groups[4].Value;
            msg.Sent = m.Groups[5].Value;
            msg.Message = m.Groups[6].Value;
            messages.Add(msg);

            m = m.NextMatch();
        }
    }
    catch (Exception ex)
    {
        //throw ex;
    }
    return messages;
}

Hope you can help me with this problem.

Thank you.
Posted
Updated 15-Oct-15 4:45am
v2

Replace the double \ by one \. You are in a C# verbatim string (@"..."), so, each character is taken as is - no escaping needed (except for " which must be doubled for escaping). See also Escaping in C#: characters, strings, string formats, keywords, identifiers[^].
Cheers
Andi
 
Share this answer
 
v4
Comments
Mshadow17 15-Oct-15 17:13pm    
Hi Andi, it works. It solved my 8hour porblem. Thanks a lot.

Cheers!

Mike
Andreas Gieriet 16-Oct-15 10:39am    
You are welcome!
Cheers
Andi
This regex should work if the only thing you want is the long hex-string.

\\r\\n[0-9A-F]+\\r\\n

or
\\r\\n[0-9A-F]{10,}\\r\\n  //At least 10 characters


You can probably skip the \r\n as well.
 
Share this answer
 
v2
Comments
Mshadow17 15-Oct-15 17:14pm    
Thanks George for sharing this idea.

Mike

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