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 to share