Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Parse hostname from HTML header string

by JoeJaz (Monk)
on Oct 02, 2004 at 22:23 UTC ( [id://395935]=perlquestion: print w/replies, xml ) Need Help??

JoeJaz has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am not very good at regular expressions and am looking for a way to parse out the hostname from a HTML header string. My input string looks like this: "Host: localhost:7799" What makes this difficult for me is that the host string might contain an IP address, a hostname containing periods, and might not even contain a port number at all. Also, I am not sure if the space between Host: and localhost will always be there. Does anyone have some tips on how I might go about parsing this out? Thanks for taking the time to read this. Have a nice day, Joe

20041002 Edit by castaway: Changed title from 'Reg eX'

Replies are listed 'Best First'.
Re: Parse hostname from HTML header string
by bart (Canon) on Oct 02, 2004 at 23:01 UTC
    I'm assuming that a host name can't start with whitespace, or even contain whitespace, and annot contain a colon. I'm also assuming a default value of 80 for the port number.

    Just for this example, I'm assuming that string is in $_.

    my($host, $port) = /^Host:\s*([^:\s]+)(?:\:(\d+))?/i; $port = 80 if not defined $port;
    You see, if the ":7799" part is missing, $2 (= the value assigned to $port) will be undef. And then, I assign 80 to it.
      Very clever. I didn't know you could assign output of Reg Ex's that way. Interesting. I hope my class in Programming Languages helps me learn some Reg Ex's more thoroughly. In the mean time, thanks for the nice example and for your help with my problem. Joe
Re: Reg eX
by tachyon (Chancellor) on Oct 03, 2004 at 01:39 UTC
    my ( $host, $port ) = $header =~ m/^Host:\s*([A-Za-z0-9\.\-]+):?(\d*)/ +mi;

    cheers

    tachyon

      Hi, Thank you for providing this awsome regular expression. Looks like just what I need to do. Joe

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://395935]
Approved by bart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-23 18:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found