Retrieve text from a text

  • Thread starter Thread starter Maya
  • Start date Start date
M

Maya

Hello Everybody,

I have an application that receives a text in this format:

Received from john [227.27.34.12] by server.com with ESMTP (SMTPD32-8.05) id
AD5258A0142; Sun, 01 Aug 2004 02:54:26 +0100 From: "John smith" To: "maya"
Subject: Test message Date: Sun, 1 Aug 2004 03:00:43 +0100 MIME-Version: 1.0
Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit
X-Mailer: Microsoft Office Outlook, Build 11.0.5510 X-MimeOLE: Produced By
Microsoft MimeOLE V6.00.3790.181 Thread-Index Body: How are you?

Is it possible to pull specific text that I only need from the above text
and assign it to string variables like this:

TextBox1.Text = From: "John smith"
TextBox2.Text = Subject: Test message
TextBox3.Text = Body: How are you?

Keep in mind that strings "From:" and "Subject:" and "Body:" are fixed
strings contained all the time in the original text.

Thanks for your help.
 
Hi Maya,

You could try regular expressions (System.Text.RegularExpressions), similar
to this:

string message = @"
Received from john [227.27.34.12] by server.com with ESMTP (SMTPD32-8.05) id
AD5258A0142; Sun, 01 Aug 2004 02:54:26 +0100 From: ""John smith"" To:
""maya""
Subject: Test message Date: Sun, 1 Aug 2004 03:00:43 +0100 MIME-Version: 1.0
Content-Type: text/plain; charset=""us-ascii"" Content-Transfer-Encoding:
7bit
X-Mailer: Microsoft Office Outlook, Build 11.0.5510 X-MimeOLE: Produced By
Microsoft MimeOLE V6.00.3790.181 Thread-Index Body: How are you?
";
Regex regex = new
Regex(@"\sFrom\:[\s]+\""(?<from>(.)+)\""\s+To\:[\s]+\""(?<to>(.)+)\""\s+Subj
ect\:[\s]+(?<subject>(.)+)Date\:(\s|\S)+\sBody\:[\s]+(?<body>(.)+)\r\n");
Match match = regex.Match(message);

txtFrom.Text = match.Groups["from"].ToString();
txtTo.Text = match.Groups["to"].ToString();
txtSubject.Text = match.Groups["subject"].ToString();
txtBody.Text = match.Groups["body"].ToString();

Joe
 
Thank you Joe for your reply,

I did try your suggested code, the code runs fine with no errors, however i
have been trying it with different texts and for some reasons it didnt
return any matches, the match.Success function always returns "false" to me
as there are no matching occures.

Any idea?

Thank you so much.

Maya.

Joe Mayo said:
Hi Maya,

You could try regular expressions (System.Text.RegularExpressions), similar
to this:

string message = @"
Received from john [227.27.34.12] by server.com with ESMTP (SMTPD32-8.05) id
AD5258A0142; Sun, 01 Aug 2004 02:54:26 +0100 From: ""John smith"" To:
""maya""
Subject: Test message Date: Sun, 1 Aug 2004 03:00:43 +0100 MIME-Version: 1.0
Content-Type: text/plain; charset=""us-ascii"" Content-Transfer-Encoding:
7bit
X-Mailer: Microsoft Office Outlook, Build 11.0.5510 X-MimeOLE: Produced By
Microsoft MimeOLE V6.00.3790.181 Thread-Index Body: How are you?
";
Regex regex = new
Regex(@"\sFrom\:[\s]+\""(? said:
ect\:[\s]+(?<subject>(.)+)Date\:(\s|\S)+\sBody\:[\s]+(?<body>(.)+)\r\n");
Match match = regex.Match(message);

txtFrom.Text = match.Groups["from"].ToString();
txtTo.Text = match.Groups["to"].ToString();
txtSubject.Text = match.Groups["subject"].ToString();
txtBody.Text = match.Groups["body"].ToString();

Joe
--
Joe Mayo, Author/Instructor
Need C#/.NET training?
visit www.mayosoftware.com
C# Tutorial - www.csharp-station.com



Maya said:
Hello Everybody,

I have an application that receives a text in this format:

Received from john [227.27.34.12] by server.com with ESMTP
(SMTPD32-8.05)
id
AD5258A0142; Sun, 01 Aug 2004 02:54:26 +0100 From: "John smith" To: "maya"
Subject: Test message Date: Sun, 1 Aug 2004 03:00:43 +0100 MIME-Version: 1.0
Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit
X-Mailer: Microsoft Office Outlook, Build 11.0.5510 X-MimeOLE: Produced By
Microsoft MimeOLE V6.00.3790.181 Thread-Index Body: How are you?

Is it possible to pull specific text that I only need from the above text
and assign it to string variables like this:

TextBox1.Text = From: "John smith"
TextBox2.Text = Subject: Test message
TextBox3.Text = Body: How are you?

Keep in mind that strings "From:" and "Subject:" and "Body:" are fixed
strings contained all the time in the original text.

Thanks for your help.
 
Hi Maya,

You should get a Regular Expression parsing tool, which will let you build
up your regular expressions and then plug them into your code. Here's a url
to a few:

http://msdn.microsoft.com/vcsharp/team/tools/default.aspx

Here's a formal reference to regular expressions, which is part of the .NET
Framework Documentation:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/cpref_start.asp

Here's a reference to Dan Appleman's "Regular Expressions with .NET" eBook:

http://www.amazon.com/exec/obidos/t...=sr_1_21/002-8169070-4960868?v=glance&s=books

Here's an excellent site with many pre-made regular expressions and
references to more information:

http://www.regexlib.com/

Joe
--
Joe Mayo, Author/Instructor
Need C#/.NET training?
visit www.mayosoftware.com
C# Tutorial - www.csharp-station.com


Maya said:
Thank you Joe for your reply,

I did try your suggested code, the code runs fine with no errors, however i
have been trying it with different texts and for some reasons it didnt
return any matches, the match.Success function always returns "false" to me
as there are no matching occures.

Any idea?

Thank you so much.

Maya.

Joe Mayo said:
Hi Maya,

You could try regular expressions (System.Text.RegularExpressions), similar
to this:

string message = @"
Received from john [227.27.34.12] by server.com with ESMTP
(SMTPD32-8.05)
id
AD5258A0142; Sun, 01 Aug 2004 02:54:26 +0100 From: ""John smith"" To:
""maya""
Subject: Test message Date: Sun, 1 Aug 2004 03:00:43 +0100 MIME-Version: 1.0
Content-Type: text/plain; charset=""us-ascii"" Content-Transfer-Encoding:
7bit
X-Mailer: Microsoft Office Outlook, Build 11.0.5510 X-MimeOLE: Produced By
Microsoft MimeOLE V6.00.3790.181 Thread-Index Body: How are you?
";
Regex regex = new
ect\:[\s]+(? said:
Match match = regex.Match(message);

txtFrom.Text = match.Groups["from"].ToString();
txtTo.Text = match.Groups["to"].ToString();
txtSubject.Text = match.Groups["subject"].ToString();
txtBody.Text = match.Groups["body"].ToString();

Joe
--
Joe Mayo, Author/Instructor
Need C#/.NET training?
visit www.mayosoftware.com
C# Tutorial - www.csharp-station.com



Maya said:
Hello Everybody,

I have an application that receives a text in this format:

Received from john [227.27.34.12] by server.com with ESMTP
(SMTPD32-8.05)
id
AD5258A0142; Sun, 01 Aug 2004 02:54:26 +0100 From: "John smith" To: "maya"
Subject: Test message Date: Sun, 1 Aug 2004 03:00:43 +0100
MIME-Version:
1.0
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding:
7bit
X-Mailer: Microsoft Office Outlook, Build 11.0.5510 X-MimeOLE:
Produced
 
Thanks you Joe for that, very kind of you.

Maya

Joe Mayo said:
Hi Maya,

You should get a Regular Expression parsing tool, which will let you build
up your regular expressions and then plug them into your code. Here's a url
to a few:

http://msdn.microsoft.com/vcsharp/team/tools/default.aspx

Here's a formal reference to regular expressions, which is part of the ..NET
Framework Documentation:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/cpref_start.asp

Here's a reference to Dan Appleman's "Regular Expressions with .NET" eBook:http://www.amazon.com/exec/obidos/t...=sr_1_21/002-8169070-4960868?v=glance&s=books

Here's an excellent site with many pre-made regular expressions and
references to more information:

http://www.regexlib.com/

Joe
--
Joe Mayo, Author/Instructor
Need C#/.NET training?
visit www.mayosoftware.com
C# Tutorial - www.csharp-station.com


Maya said:
Thank you Joe for your reply,

I did try your suggested code, the code runs fine with no errors,
however
i
have been trying it with different texts and for some reasons it didnt
return any matches, the match.Success function always returns "false" to me
as there are no matching occures.

Any idea?

Thank you so much.

Maya.

Hi Maya,

You could try regular expressions (System.Text.RegularExpressions), similar
to this:

string message = @"
Received from john [227.27.34.12] by server.com with ESMTP
(SMTPD32-8.05)
id
AD5258A0142; Sun, 01 Aug 2004 02:54:26 +0100 From: ""John smith"" To:
""maya""
Subject: Test message Date: Sun, 1 Aug 2004 03:00:43 +0100
MIME-Version:
1.0
Content-Type: text/plain; charset=""us-ascii"" Content-Transfer-Encoding:
7bit
X-Mailer: Microsoft Office Outlook, Build 11.0.5510 X-MimeOLE:
Produced
Regex(@"\sFrom\:[\s]+\""(? said:
ect\:[\s]+(? said:
Match match = regex.Match(message);

txtFrom.Text = match.Groups["from"].ToString();
txtTo.Text = match.Groups["to"].ToString();
txtSubject.Text = match.Groups["subject"].ToString();
txtBody.Text = match.Groups["body"].ToString();

Joe
--
Joe Mayo, Author/Instructor
Need C#/.NET training?
visit www.mayosoftware.com
C# Tutorial - www.csharp-station.com



Hello Everybody,

I have an application that receives a text in this format:

Received from john [227.27.34.12] by server.com with ESMTP (SMTPD32-8.05)
id
AD5258A0142; Sun, 01 Aug 2004 02:54:26 +0100 From: "John smith" To: "maya"
Subject: Test message Date: Sun, 1 Aug 2004 03:00:43 +0100 MIME-Version:
1.0
Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding:
7bit
X-Mailer: Microsoft Office Outlook, Build 11.0.5510 X-MimeOLE:
Produced
By
Microsoft MimeOLE V6.00.3790.181 Thread-Index Body: How are you?

Is it possible to pull specific text that I only need from the above text
and assign it to string variables like this:

TextBox1.Text = From: "John smith"
TextBox2.Text = Subject: Test message
TextBox3.Text = Body: How are you?

Keep in mind that strings "From:" and "Subject:" and "Body:" are fixed
strings contained all the time in the original text.

Thanks for your help.
 
Back
Top