Reputation: 91
I am trying to get a return message from 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 balance details, please send BAL to 1"
My expected result is:
00302220100 Your main balance is 10K, valid until 23/10/2015. For more balance details, please 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. Thanks a lot.
Upvotes: 3
Views: 1091
Reputation: 67968
(?<=AT\+[\s\S]*?CUSD:[^"]*")[^"]*
You can make 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
}
}
Upvotes: 1
Reputation: 626835
You can use
(?<=\+CUSD:\s+2,")[^"]+
In C#, declare as:
var rx = new Regex(@"(?<=\+CUSD:\s+2,"")[^""]+");
The (?<=\+CUSD:\s+2,")
look-behind will check the right position in the string, and the expected output will reside in the m.Value
.
Here is a demo
Upvotes: 1
Reputation: 13059
You need to remove the AT
from your regular expression.
Try \+CUSD: (\d+),"(.*?)"
new Regex("\\+CUSD: (\\d+),\"(.*?)\"", RegexOptions.Singleline);
Upvotes: 0