Tap:
I'm not sure that I entirely understand the question. If you mean "how do I
get to the groups that the the regex splits out", realize that you can
access the groups by name. If that's the problem you should be able to cut
things down to just a couple lines of code:
(something like this, but it's off the cuff, so it might not compile without
adjustment):
{
string pattern = "(.*?ID

(?<ID>.*) to
(?<toAddress>.*?)(?<ccString>{CC}.*)";
MatchCollection mn = Regex.Matches(stringToParse, pattern);
// note: haven't tried with multiple strings, just your one test string
foreach(Match match in mn)
{
string idString = match.Groups["ID"].Value;
string toAddress = match.Groups["toAddress"].Value;
string CCline = match.Groups["ccString"].Value;
}
In the expression that I built, when you see a "(?<someName> some pattern)"
all the text in some pattern can be retrieved form the match object's Groups
collection as a group named "someName"). You can retreive groups by name or
by index. Note that match.Groups[0] always contains the entire match.
There _should_ be one match object in the match collection per line in the
string that you are parsing, assuming that each line is delimmited by a
carriage return/linefeed (haven't tested that part, but I think that's
right).
I hope this answers your question. If not, post back.
-- Jeremy
Tap said:
Thanks for the expression. I can get to work till I get the position at
which the string is found, but How do I read the content of the string in
between or at a given position.
e.g. This is what I am currently doing, and it gives me the result, but not happy about the code.
Regex r = new Regex("((ID

|( to )|({CC}))");
MatchCollection mn = r.Matches(stringToParse);
for (int i=0;i<mn.Count;i++)
{
switch (mn
.Value)
{
case "ID:":
tmpEscalation.MessageIDValue = (stringToParse).Substring((mn.Index)+3,
((mn[i+1].Index)-(mn.Index))-3);
break;
case " to ":
if (mn.Count > 2)
{
tmpEscalation.sentTo = (stringToParse).Substring((mn.Index)+4, ((mn[i+1].Index)-(mn.Index))-4);
}
else
{
tmpEscalation.sentTo = (stringToParse).Substring((mn.Index)+4);
}
break;
case "{CC}":
if (tmpEscalation.ccToEmail == null)
{
tmpEscalation.ccToEmail = (stringToParse).Substring((mn.Index)+4);
}
break;
}
}
-----------------------------------------
I know the code is annoying, but just trying to improve, so please bear with me.
Thanks,
Tap