Regular expressions

D

Darwin

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.
 
G

Gilles Kohl [MVP]

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.

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.
 
S

schoensy

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 textwww.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.

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.
 
D

Darwin

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);

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top