Regular expression to parse AT command response

I am trying to get a return message from the AT command response.

This is the input:

AT + CUSD = 1, "* 124 #", 15

OK

+ CUSD: 2, "00302220100 Your main balance is 10K, valid until 23/10/2015. For more information on the balance, send BAL to 1"

Expected Result:

00302220100 Your main balance is 10K, valid until 23/10/2015. For more information on the balance, send BAL to 1

Here is my code:

    private string ParseMessages_ChkCredit(string input)
    {
        string messages = "";
        Regex r = new Regex("\\AT+CUSD: (\\d+),\"(.*?)\"", RegexOptions.Singleline);
        Match m = r.Match(input);
        while (m.Success)
        {
            messages = m.Groups[2].Value.ToString();
            break;
        }
        return messages;
    }

      

The regular expression does not match. Please kindly help me. Many thanks.

+3


source to share


4 answers


(?<=AT\+[\s\S]*?CUSD:[^"]*")[^"]*

      

You can use variable lookbehind

. See demo .



string strRegex = @"(?<=AT\+[\s\S]*?CUSD:[^""]*"")[^""]*";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = @"AT+CUSD=1,""*124#"",15" + "\n" + @"OK" + "\n\n" + @"+CUSD: 2,""00302220100 Your main balance is 10K, valid until 23/10/2015. For more balance details, please send BAL to 1""" + "\n";

foreach (Match myMatch in myRegex.Matches(strTargetString))
{
  if (myMatch.Success)
  {
    // Add your code here
  }
}

      

+1


source


You can try this regex:

(?<=\+CUSD\: 2,\")(.+)

      



Live Demo

+1


source


you can use

(?<=\+CUSD:\s+2,")[^"]+

      

In C # declare as:

var rx = new Regex(@"(?<=\+CUSD:\s+2,"")[^""]+");

      

(?<=\+CUSD:\s+2,")

look-behind checks the correct line position and the expected output will be in m.Value

.

Here is a demo

+1


source


You need to remove AT

from your regex.

Try \+CUSD: (\d+),"(.*?)"

new Regex("\\+CUSD: (\\d+),\"(.*?)\"", RegexOptions.Singleline);

      

0


source







All Articles