473,473 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

System.Text.RegEx

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

stringValue = "Email Message ID:TAPASVI to ta*****@hotmail.com;ta******@cs.com;...**@hotmail.com"

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 = "ta*****@hotmail.com;ta******@cs.com;"

3) Take all the {CC} as one string
i.e. stringVal3 = "{C**********@hotmail.com;{C*************@hotmail. com"

-----------------------------------

Thanks,

Tapasvi

Nov 15 '05 #1
5 3051
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.

"Tap" <an*******@discussions.microsoft.com> wrote in message
news:36**********************************@microsof t.com...
I would like to parse the following string with as little code as possible.
stringValue = "Email Message ID:TAPASVI to ta*****@hotmail.com;ta******@cs.com;...@h otmail.com;{CC}tapstemp007
@hotmail.com"
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 = "ta*****@hotmail.com;ta******@cs.com;"

3) Take all the {CC} as one string
i.e. stringVal3 = "{C**********@hotmail.com;{C*************@hotmail. com"
-----------------------------------

Thanks,

Tapasvi

Nov 15 '05 #2
Tap
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[i].Value

case "ID:"
tmpEscalation.MessageIDValue = (stringToParse).Substring((mn[i].Index)+3, ((mn[i+1].Index)-(mn[i].Index))-3)
break

case " to "
if (mn.Count > 2

tmpEscalation.sentTo = (stringToParse).Substring((mn[i].Index)+4, ((mn[i+1].Index)-(mn[i].Index))-4)

els

tmpEscalation.sentTo = (stringToParse).Substring((mn[i].Index)+4)

break

case "{CC}"
if (tmpEscalation.ccToEmail == null

tmpEscalation.ccToEmail = (stringToParse).Substring((mn[i].Index)+4)

break

----------------------------------------
I know the code is annoying, but just trying to improve, so please bear with me

Thanks

Ta
Nov 15 '05 #3
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" <an*******@discussions.microsoft.com> wrote in message
news:7D**********************************@microsof t.com...
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[i].Value)
{
case "ID:":
tmpEscalation.MessageIDValue = (stringToParse).Substring((mn[i].Index)+3,
((mn[i+1].Index)-(mn[i].Index))-3); break;

case " to ":
if (mn.Count > 2)
{
tmpEscalation.sentTo = (stringToParse).Substring((mn[i].Index)+4, ((mn[i+1].Index)-(mn[i].Index))-4); }
else
{
tmpEscalation.sentTo = (stringToParse).Substring((mn[i].Index)+4);
}
break;

case "{CC}":
if (tmpEscalation.ccToEmail == null)
{
tmpEscalation.ccToEmail = (stringToParse).Substring((mn[i].Index)+4);
}
break;
}
}

-----------------------------------------
I know the code is annoying, but just trying to improve, so please bear with me.
Thanks,

Tap

Nov 15 '05 #4
Tap
Jeremy

Thats exactly I was looking for. Thank you very much.
Nov 15 '05 #5
Glad it worked out for you.

Regards

-- Jeremy

"Tap" <an*******@discussions.microsoft.com> wrote in message
news:D7**********************************@microsof t.com...
Jeremy,

Thats exactly I was looking for. Thank you very much.

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Sam Lowry | last post by:
Greetings. I am trying to do something which should elementary for Perl, but I have only been able to find bits and pieces on it. When I put the bits together they do not work. Maybe I am going...
8
by: Beznas | last post by:
Hi All; I'm trying to create an ASP function called CleanX that removes the punctuation and some characters like (*&^%$#@!<>?"}|{..) from a text string I came up with this but It...
10
by: Dave | last post by:
I can't get a number out of my textbox. it keeps giving me an error and saying that the input string was not in the correct format. this.number = int.Parse((this.numberTextBox.Text.ToString()); ...
1
by: jason | last post by:
I have exhausted all resources, so perhaps someone out there can help. I have a 8MB string (that represents an html document) and I am trying to run a couple of regular expressions on this...
10
by: Chance Hopkins | last post by:
I'm trying to match a set of matches after some initial text: mytext: "something" "somethingelse" "another thing" "maybe another" (?:mytext: )(?<mymatch>{1,1}+{1,1}+)+ I only get the last one...
5
by: D | last post by:
hi there , i want to do something fairly simple (well it was simple in PERL) using the replace function of Regex... but i cannot find the docs to help me on it... i want to use a regex to find a...
8
by: Richard Lionheart | last post by:
Hi All, I tried using RegEx, but the compiler barfed with "The type of namespace 'RegEx' could not be found. Prior to this, I had the same problem with MatchCollection, but discovered it's...
17
by: J.S. | last post by:
I have a text file with parameters like the following embedded in the text: @@TextBox1@@, @@TextBox2@@, etc. I know how to read this text file. However, I am trying to figure out how to...
2
by: Tim_Mac | last post by:
hi, i have a tricky problem and my regex expertise has reached its limit. i have read other posts on this newsgroup that pull out the plain text from a html string, but that won't work for me...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.