in reply to Parse hostname from HTML header string

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.

Replies are listed 'Best First'.
Re^2: Parse hostname from HTML header string
by JoeJaz (Monk) on Oct 02, 2004 at 23:55 UTC
    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