# Validates\_format\_of :url, regexp?

**URL:** <https://www.ruby-forum.com/t/validates-format-of-url-regexp/71613>\
**Category:** Rails\
**Created:** [February 21, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/validates-format-of-url-regexp/71613 "2006-02-21T22:38:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [February 21, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/validates-format-of-url-regexp/71613/1 "2006-02-21T22:38:52Z")

</div>

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.,;?'\+&%$#=~\_-]+))_$

([Solved: URL Regular Expression | Experts Exchange](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/url3.regex)  
[http://www.foad.org/~abigail/Perl/url2.html](http://www.foad.org/~abigail/Perl/url2.html)

Thanks,  
Joe

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [February 22, 2006, 6:34pm UTC](https://www.ruby-forum.com/t/validates-format-of-url-regexp/71613/2 "2006-02-22T18:34:43Z")

</div>

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.,;?'\+&%$#=~\_-]+))_$
> 
> ([Solved: URL Regular Expression | Experts Exchange](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](http://www.foad.org/~abigail/Perl/url3.regex)  
> Ick! Mind-bleach, quick!

–  
Alex

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [February 25, 2006, 5:38pm UTC](https://www.ruby-forum.com/t/validates-format-of-url-regexp/71613/3 "2006-02-25T17:38:54Z")

</div>

On 2/21/06, Joe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [http://www.foad.org/~abigail/Perl/url3.regex](http://www.foad.org/~abigail/Perl/url3.regex)  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](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.
