Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Regular expressions

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Darwin - 24 Mar 2008 21:28 GMT
Hi,
I am trying to extract the host name from a HTTP header:
GET /1/1/typical.php HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/x-shockwave-flash, */*
Referer: http://www.httphost.com/1/1/
Accept-Language: en-us
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR
1.1.4322; .NET CLR 2.0.50727)
Host: www.httphost.com
Connection: Keep-AliveSo I just want to extract the text www.httphost.com 
from the above. My code is below:
Regex r = new Regex(@"Host: ([A-Za-z0-9_:.]+)$", RegexOptions.Multiline);

// Match the regular expression pattern against a text string.

Match m = r.Match(message.ToString());

MessageBox.Show(m.Value);

But it shows a blank, I am sure there is an obvious answer.
Gilles Kohl [MVP] - 24 Mar 2008 22:00 GMT
>Hi,
>I am trying to extract the host name from a HTTP header:
[quoted text clipped - 19 lines]
>
>But it shows a blank, I am sure there is an obvious answer.

Hmm, can you post a complete snippet of code? (e.g. a self-contained method)
What is message, and what is its exact contents?

What does m.Success contain?

  Regards,
  Gilles.
schoensy - 24 Mar 2008 22:07 GMT
> Hi,
> I am trying to extract the host name from a HTTP header:
[quoted text clipped - 19 lines]
>
> But it shows a blank, I am sure there is an obvious answer.

I was able to validate that the following regexp works, but I haven't
run it through the code you wrote to see what the result is. Just
winging it, here's how I would write it out:

Match m = new Regex.Match(message.ToString(), @"Host: ([\w\.\-:_]*)");
MessageBox.Show(m.Value);

Hope that helps.
Darwin - 24 Mar 2008 23:04 GMT
Thanks for the help here's what I ended up with (appropriate error checks to
be implemented) :

Regex r = new Regex(@"Host: ([\w\.\-:_]*)", RegexOptions.Multiline);

Match m = r.Match(message.ToString());

if (m.Length > 0)

{

MessageBox.Show(m.Groups[1].Value);

}

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.