Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Splitting up hostname using Regex

Reply
Thread Tools

Splitting up hostname using Regex

 
 
Gu stav
Guest
Posts: n/a
 
      01-15-2008
Hi! I'm trying to write a regex that splits up a hostname and returns it
in a nice array. For example:

"eat.chunky.bacon.com" => regex => ["eat","chunky","bacon","com"]

Been trying to wrap my head around this, but I merely get around to
catching the top domain Anyone done this recently?

Many thanks!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Thomas Adam
Guest
Posts: n/a
 
      01-15-2008
Hi --

On 15/01/2008, Gu stav <> wrote:
> Hi! I'm trying to write a regex that splits up a hostname and returns it
> in a nice array. For example:
>
> "eat.chunky.bacon.com" => regex => ["eat","chunky","bacon","com"]
>
> Been trying to wrap my head around this, but I merely get around to
> catching the top domain Anyone done this recently?


Why a regexp?

>> "eat.chunky.bacon.com".split(/\./)

=> ["eat", "chunky", "bacon", "com"]

The above will fail for foo.co.uk, but that's your problem.

-- Thomas Adam

 
Reply With Quote
 
 
 
 
Justin Collins
Guest
Posts: n/a
 
      01-15-2008
Gu stav wrote:
> Hi! I'm trying to write a regex that splits up a hostname and returns it
> in a nice array. For example:
>
> "eat.chunky.bacon.com" => regex => ["eat","chunky","bacon","com"]
>
> Been trying to wrap my head around this, but I merely get around to
> catching the top domain Anyone done this recently?
>
> Many thanks!
>


No need for regex or anything fancy, really...

irb(main):001:0> "eat.chunky.bacon.com".split(".")
=> ["eat", "chunky", "bacon", "com"]


-Justin



 
Reply With Quote
 
Gu stav
Guest
Posts: n/a
 
      01-15-2008

> Why a regexp?


Good question, to which I have no real answer

Thanks a million!

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Windham, Kristopher R.
Guest
Posts: n/a
 
      01-15-2008
Hey,
is there some sort of humor I am not getting here?
why does it fail for foo.co.uk?

irb(main):001:0> "eat.chunky.bacon.com".split(/\./)
=> ["eat", "chunky", "bacon", "com"]
irb(main):002:0> "foo.co.uk".split(/\./)
=> ["foo", "co", "uk"]

On Jan 14, 2008, at 8:27 PM, Thomas Adam wrote:

>>> "eat.chunky.bacon.com".split(/\./)

> => ["eat", "chunky", "bacon", "com"]
>
> The above will fail for foo.co.uk, but that's your problem.



 
Reply With Quote
 
Thomas Adam
Guest
Posts: n/a
 
      01-15-2008
Hello --

On 15/01/2008, Windham, Kristopher R. <> wrote:
> Hey,
> is there some sort of humor I am not getting here?
> why does it fail for foo.co.uk?


Depends what the OP was wanting to do with the TLD. You might not
want to split up the ".co.uk" part into the constituent components,
but rather keep it as ".co.uk".

Are you laughing now?

-- Thomas Adam

 
Reply With Quote
 
Windham, Kristopher R.
Guest
Posts: n/a
 
      01-15-2008
Oh,
I see
I am laughing now
but not at that..
at my own shortcomings..
That does make sense..



On Jan 14, 2008, at 9:08 PM, Thomas Adam wrote:

> Hello --
>
> On 15/01/2008, Windham, Kristopher R. <> wrote:
>> Hey,
>> is there some sort of humor I am not getting here?
>> why does it fail for foo.co.uk?

>
> Depends what the OP was wanting to do with the TLD. You might not
> want to split up the ".co.uk" part into the constituent components,
> but rather keep it as ".co.uk".
>
> Are you laughing now?
>
> -- Thomas Adam
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
RMI: take ip/hostname what client was using and give it back as aremote objects hostname AWieminer Java 0 07-12-2005 08:05 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Dibling C++ 0 07-19-2003 04:41 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? Mark C++ 0 07-19-2003 04:24 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Ericson C++ 0 07-19-2003 04:03 PM



Advertisments