Validates_format_of :url, regexp?


#1

Hey,

Does anybody have a regexp for validating URLs? I found this one and am
trying to adapt it:

^(((ht|f)tp(s?))://)?(www.|[a-zA-Z].)[a-zA-Z0-9-.]+.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(:[0-9]+)(/($|[a-zA-Z0-9.,;?’\+&%$#=~_-]+))$

(http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21245168.html)

I also found this one, but, um, well…
http://www.foad.org/~abigail/Perl/url3.regex
http://www.foad.org/~abigail/Perl/url2.html

Thanks,
Joe


#2

Joe wrote:

Hey,

Does anybody have a regexp for validating URLs? I found this one and am
trying to adapt it:

^(((ht|f)tp(s?))://)?(www.|[a-zA-Z].)[a-zA-Z0-9-.]+.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(:[0-9]+)(/($|[a-zA-Z0-9.,;?’\+&%$#=~_-]+))$

(http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21245168.html)
It depends what rules you’re trying to validate against. The simplest
thing to do is probably to use the regex from RFC 2396:

(([^:/?#]+):)?(//([^/?#]))?([^?#])(?([^#]))?(#(.))

and then split out the component parts and validate them separately.
That way you can check that the server name has a reasonable tld, and
that the scheme matches something valid, and so on. Check the rfc for
details.

I also found this one, but, um, well…
http://www.foad.org/~abigail/Perl/url3.regex
Ick! Mind-bleach, quick!


Alex


#3

On 2/21/06, Joe removed_email_address@domain.invalid wrote:

http://www.foad.org/~abigail/Perl/url3.regex
http://lists.rubyonrails.org/mailman/listinfo/rails

Hello,

You remembered me that I wanted to have a RFC-compliant URI regexp
Well, this is done now, I’ve just translated the RFC into some ruby
code to make a regexp string
See the attachement.