[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Splitting up hostname using Regex

Gu stav

1/15/2008 1:23:00 AM

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

6 Answers

Thomas Adam

1/15/2008 1:28:00 AM

0

Hi --

On 15/01/2008, Gu stav <gustav@vonsydow.tv> 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

Justin Collins

1/15/2008 1:38:00 AM

0

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



Gu stav

1/15/2008 1:40:00 AM

0


> Why a regexp?

Good question, to which I have no real answer ;)

Thanks a million!

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

Windham, Kristopher R.

1/15/2008 1:59:00 AM

0

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. :)


Thomas Adam

1/15/2008 2:08:00 AM

0

Hello --

On 15/01/2008, Windham, Kristopher R. <kriswindham@gmail.com> 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

Windham, Kristopher R.

1/15/2008 2:18:00 AM

0

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. <kriswindham@gmail.com> 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
>