Forum: Ruby regex hostnames?

Posted by skolo pen (skolopen)
on 2013-01-15 22:26
Not versed in regex, hopefully someone can help me out.

How can I return only the domain from a string like

host[0-9].temp.temp.com
or
[0-9]host.temp.com


So it only returns "temp.temp.com" or "temp.com". Essentially removing
the shortname of the host even if there is a range within the shortname.
I may have additional periods to identify subdomains but there will
never be a period inside a the shortname of the host.
Posted by "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2013-01-15 23:34
(Received via mailing list)
On Tue, Jan 15, 2013 at 10:26 PM, skolo pen <lists@ruby-forum.com> 
wrote:
> the shortname of the host even if there is a range within the shortname.
> I may have additional periods to identify subdomains but there will
> never be a period inside a the shortname of the host.

So you want to remove everything up to the first period?

s = "host1.temp.temp.com"
s[/\..*/,1]

http://ruby-doc.org/core-1.9.3/String.html#method-i-5B-5D

Jesus.
Posted by Joel Pearson (virtuoso)
on 2013-01-16 00:20
>
> s = "host1.temp.temp.com"
> s[/\..*/,1]

Shouldn't that be s[/\..*/,0] or s[/\..*/] ?
Posted by Matthew Kerwin (mattyk)
on 2013-01-16 00:38
(Received via mailing list)
On 16 January 2013 09:20, Joel Pearson <lists@ruby-forum.com> wrote:

> >
> > s = "host1.temp.temp.com"
> > s[/\..*/,1]
>
> Shouldn't that be s[/\..*/,0] or s[/\..*/] ?
>
>
Or if you don't want the dot:

    s[/\.(.*)/,1]


--
  Matthew Kerwin, B.Sc (CompSci) (Hons)
  http://matthew.kerwin.net.au/
  ABN: 59-013-727-651

  "You'll never find a programming language that frees
  you from the burden of clarifying your ideas." - xkcd
Posted by "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2013-01-16 09:01
(Received via mailing list)
On Wed, Jan 16, 2013 at 12:20 AM, Joel Pearson <lists@ruby-forum.com> 
wrote:
>>
>> s = "host1.temp.temp.com"
>> s[/\..*/,1]

Oops, copy/paste error, I forgot the parens. This was my meant solution:

1.9.2p290 :003 > s[/\.(.*)/,1]
 => "temp.temp.com"

> Shouldn't that be s[/\..*/,0] or s[/\..*/] ?

These ones return also the first ".":

1.9.2p290 :004 > s[/\..*/,0]
 => ".temp.temp.com"
1.9.2p290 :005 > s[/\..*/]
 => ".temp.temp.com"

Jesus.
Posted by "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2013-01-16 09:02
(Received via mailing list)
On Wed, Jan 16, 2013 at 12:36 AM, Matthew Kerwin <matthew@kerwin.net.au> 
wrote:
>
>     s[/\.(.*)/,1]

Exactly, that was what I meant to write...

Jesus.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.