Reputation: 1617
I'm trying to get the message string out from this VMG file. I only want to strings after the Date line and before "END:VBODY"
The best I got so far is this regex string BEGIN:VBODY([^\n]*\n+)+END:VBODY
Anyone can help refine it?
N:
TEL:+65123345
END:VCARD
BEGIN:VENV
BEGIN:VBODY
Date:8/11/2013 11:59:00 PM
thi is a test message
Hello this is a test message on line 2
END:VBODY
END:VENV
END:VENV
END:VMSG
Upvotes: 1
Views: 171
Reputation: 1045
Here is a solution using Regular Expressions,
string text = @"N:
TEL:+65123345
END:VCARD
BEGIN:VENV
BEGIN:VBODY
Date:8/11/2013 11:59:00 PM
thi is a test message
Hello this is a test message on line 2
END:VBODY
END:VENV
END:VENV
END:VMSG";
string pattern = @"BEGIN:VBODY(?<Value>[a-zA-Z0-9\r\n.\S\s ]*)END:VBODY";//Pattern to match text.
Regex rgx = new Regex(pattern, RegexOptions.Multiline);//Initialize a new Regex class with the above pattern.
Match match = rgx.Match(text);//Capture any matches.
if (match.Success)//If a match is found.
{
string value2 = match.Groups["Value"].Value;//Capture match value.
MessageBox.Show(value2);
}
Demo here.
and now a non-regex solution,
string text = @"N:
TEL:+65123345
END:VCARD
BEGIN:VENV
BEGIN:VBODY
Date:8/11/2013 11:59:00 PM
thi is a test message
Hello this is a test message on line 2
END:VBODY
END:VENV
END:VENV
END:VMSG";
int startindex = text.IndexOf("BEGIN:VBODY") + ("BEGIN:VBODY").Length;//The just start index of Date...
int length = text.IndexOf("END:VBODY") - startindex;//Length of text till END...
if (startindex >= 0 && length >= 1)
{
string value = text.Substring(startindex, length);//This is the text you need.
MessageBox.Show(value);
}
else
{
MessageBox.Show("No match found.");
}
Demo here.
Hope it helps.
Upvotes: 0
Reputation: 71538
If you want to use regex, you can modify your current regex a little, because the $0 group has what you are looking for.
BEGIN:VBODY\n?((?:[^\n]*\n+)+?)END:VBODY
Basically what happened was ([^\n]*\n+)+
turned into (?:[^\n]*\n+)+?
(turning this part lazy might be safer)
And then wrap that whole part around parens: ((?[^\n]*\n+)+?)
I added \n?
before this to make the output a little cleaner.
A non-regex solution might be something like this:
string str = @"N:
TEL:+65123345
END:VCARD
BEGIN:VENV
BEGIN:VBODY
Date:8/11/2013 11:59:00 PM
thi is a test message
Hello this is a test message on line 2
END:VBODY
END:VENV
END:VENV
END:VMSG";
int startId = str.IndexOf("BEGIN:VBODY")+11; // 11 is the length of "BEGIN:VBODY"
int endId = str.IndexOf("END:VBODY");
string result = str.Substring(startId, endId-startId);
Console.WriteLine(result);
Output:
Date:8/11/2013 11:59:00 PM
thi is a test message
Hello this is a test message on line 2
Upvotes: 1