System.Text.RegEx

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to parse the following string with as little code as possible.

stringValue = "Email Message ID:TAPASVI to (e-mail address removed);[email protected];{CC}[email protected];{CC}[email protected]"

1) Take everything in between "ID:" and the word " to"
i.e. stringVal1 = "TAPASVI"

2) Take between the word " to " and "{CC}"
i.e. stringVal2 = "(e-mail address removed);[email protected];"

3) Take all the {CC} as one string
i.e. stringVal3 = "{CC}[email protected];{CC}[email protected]"
 
Tap:

I think that this regex expression does the trick (worked in a tester app).
I'm assuming that the quotes that you put around the string that you want to
parse are not actually in the string, but were added to the message for
clarity (if not, just add the quotes to the pattern).

regex:
(.*?ID:)(?<ID>.*) to (?<toAddress>.*?)(?<ccString>{CC}.*)

That should create a group named "ID" that will contain only the "TAPASVI"
portion of the string,
a group named "toAddress", which contains everything between " to " and
"{CC}", and a group named "ccString" that contains "{CC}" and everything
after it.
 
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)

els

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

Ta
 
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
 
Back
Top