-1

I try to get information from string that return from AT+CMGR=X (where x is index of message on SIM Card)

the string is

\r\n+CMGR: \"REC UNREAD\",\"TrueID/TMN\",\"\",\"12/07/24,14:48:11+07\"
\r\n
003400310036003000300037003800350035003A0E170E230E390E210E310E190E190E35
0E4800200041002F00430020003000380030003900350037003200360036003800200E220E2D
0E140E400E070E340E190E040E070E400E2B0E250E370E2D002000340030002E0030003000200E1A0E320E17
\r\n\r\n
OK\r\n

There are useful information in this string.

REC READ for message status Date for SMS Content of SMS OK message

and know that we can pull this info with .NET Regular group.

I found the useful thread but I cannot adjust pattern in that thread to be correct pattern that fit my string.

String pattern = @"(?<a>\+CMGL):[ ](\d*),\""
                         (?<b>[^\""]*)"",
                         (?<c>\d*),(?<d>\d*),\""
                         (?<e>\+\d*)"",(?<f>\d*),\""
                         (?<g>\d*\/\d*\/\d*,\d{2}:\d{2}:\d{2}\-\d{2})\"",\""
                         (?<h>\d{2}\/\d{2}\/\d{2}),?\r\n,?
                         (?<i>\d{2}:\d{2}:\d{2}\-\d{2})\"",
                         (?<j>\d*)";

    Regex rx = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
    Match m = rx.Match(testWord);
    while (m.Success)
    {
        Console.WriteLine(m.Groups["a"].Value);
        Console.WriteLine(m.Groups["b"].Value);
        Console.WriteLine(m.Groups["c"].Value);
        Console.WriteLine(m.Groups["d"].Value);
        Console.WriteLine(m.Groups["e"].Value);
        Console.WriteLine(m.Groups["f"].Value);
        Console.WriteLine(m.Groups["g"].Value);
        Console.WriteLine(m.Groups["h"].Value);
        Console.WriteLine(m.Groups["i"].Value);
        Console.WriteLine(m.Groups["j"].Value);
        Console.WriteLine("");
        m = m.NextMatch();
    }
    Console.ReadLine();
}

from http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/d652b20d-471c-4640-967c-b5fe71e3a42c/

Thank you very much.

embarus
  • 815
  • 3
  • 14
  • 28

1 Answers1

0

Why not just find all occurrences of pattern "([^"]+)" and for status (OK) go with (\S+)\s+$

Ωmega
  • 42,614
  • 34
  • 134
  • 203
  • Thank you very much. It works but how can I also check that string start with +CMGR: and end with OK – embarus Jul 24 '12 at 14:27
  • @embarus - Check if message match pattern `^\r\n\+CMGR:.*OK\r\n$` with `/s` modifier ...if it does, then find all occurrences of `"([^"]+)"` to get paramteres. – Ωmega Jul 24 '12 at 14:30
  • It does not match with this code var subject = string.Empty; using (var streamReader = new StreamReader("balance.txt")) { subject = streamReader.ReadToEnd(); } subject = Regex.Unescape(subject); if(Regex.IsMatch(subject,@"^\r\n\+CMGR:.*OK\r\n$")) { Console.WriteLine("match"); } – embarus Jul 24 '12 at 14:42
  • @embarus - As I wrote in my comment above, you need to use `/s` modifier. The `.` in your regex does not match line breaks. You need to specify the `RegexOptions.Singleline` option to remedy that. – Ωmega Jul 24 '12 at 15:03