Q:

Rails 3 extract the domain of a link with a regex and print it in parens,

Rails -v 3.2.3
I'm working with an app that is supposed to display a description & url of a submitted link, I am using regex operators, which is something i am very new too. here is my views code:
(<%= if link.url =~ /(:\/\/) ([^\/]*)/ then $2 else "wrong URL" end %>)
however with every link i submit, the url is always wrong URL.... 
is this because $2 is the wrong regex operator? or is the /(:\/\/) ([^\/]*)/ section incorrect in Rails 3?

A:

Kill that space in the middle!  The regex you're showing expects a space between the :// and the sub.domain.tld chunks; since no URLs have that, the regex won't match anything.  The simplest change should be:
/(:\/\/)([^\/]*)/

Or, to clean it up a little more (you don't need the first pair of parentheses):
(<%= if link.url =~ /:\/\/([^\/]*)/ then $1 else "wrong URL" end %>)

Hope that helps!

