Splitting up hostname using Regex

G

Gu stav

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!
 
T

Thomas Adam

Hi --

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"]

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

-- Thomas Adam
 
J

Justin Collins

Gu said:
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
 
W

Windham, Kristopher R.

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"]

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

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

Thomas Adam

Hello --

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
 
W

Windham, Kristopher R.

Oh,
I see
I am laughing now
but not at that..
at my own shortcomings..
That does make sense..
 

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

Members online

Forum statistics

Threads
474,276
Messages
2,571,384
Members
48,073
Latest member
ImogenePal

Latest Threads

Top