Try this demo, put it in a Main method of a console application (it uses Regex so you have to add line “using System.Text.RegularExpressions;”):
string input = "AT+CMGL=\"ALL\"\n+CMGL: 0,\"REC READ\",\"+40728072005\",,\"12/06/29,13:04:26+12\"\npassword,1,ON";
var matches = Regex.Matches(
input,
@"\""(?<msisdn>\+\d*)\"",.*,\""(?<date>\d{2}\/\d{2}/\d{2},\d{2}:\d{2}:\d{2}\+\d{2})\"".*\n+(?<passwd>[^,]*),(?<itemno>\d*),(?<command>\w*)"
, RegexOptions.Multiline);
foreach (Match m in matches)
{
Console.WriteLine(m.Groups["msisdn"].Value);
Console.WriteLine(m.Groups["date"].Value);
Console.WriteLine(m.Groups["passwd"].Value);
Console.WriteLine(m.Groups["itemno"].Value);
Console.WriteLine(m.Groups["command"].Value);
}
Console.ReadKey();
Basically, regular expression searches for a defined pattern inside text.
Meaning of the expression:
\”” – that’s how a quote sign is defined, it is escaped with backslash and written twice so it doesn’t cause compiler error.
(?+\d*) – generally, bracketed expression means that you are capturing a group, this here is a named group and it’s name is specified by ?, this group is going to capture string that begins with + sign (it is escaped by backslash) followed by numbers which are represented by backslash and letter “d” (“d” comes from “digit”). Backslash is used as an escape character for characters with special meaning. Asterisk * means that character that comes before it can occur 0 or more times.
\”” – quote, this is closing quote for “msisdn” number.
, – means comma.
.* – dot means any character, and asterisk that follows it means that any character can occur 0 or more times.
(?\d{2}/\d{2}/\d{2},\d{2}:\d{2}:\d{2}+\d{2}) – named group that captures date, expression \d{2} has \d which means digit and {2} which tells regex engine that digit shoud occur two times, then comes / – it means forwardslash (it is escaped by a backslash). Afterwards come digits separated by colons and in the end plus sign and two digits after it, that’s how the date is extracted.
\n+ – \n means newline, + is a quantifier which means that newline appears one or more times.
(?[^,]*) – is a named group that captures password part, [^,] means that any character except comma should be captured, square brackets mean range of characters, ^ means negation, asterisk means that character can occur 0 or more times.
(?\d*) – is a named group that captures numbers as defined by \d*
(?\w*) – is a named group that captures word characters (letters, digits, and underscores)
8
solved How can i extract the info between ” ” ? C#