validating URL with regular expression

Hi All,

I am new to regular expressions.Even if I try it will take sometime for me to get one regular expression for URL validation.
Can anyone help me by providing a regular expression for validating URL http,https to be used with validates_format_of ?

I found this https?://([-\w.]+)+(:\d+)?(/([\w/_.]*(?\S+)?)?)? but its not working with validates_format_of.

I’ll appreciate if anyone helps me with this.



I think you’ll need to escape / by putting \ before, otherwise you’ll close the regexp too soon.
I came out with something like this:

irb(main):001:0> r=/^(https?://)?[a-z0-9]+([.-_=&+/?]?[a-z0-9]+)+$/i

=> /^(https?://)?[a-z0-9]+([.-_=&+/?]?[a-z0-9]+)+$/i
irb(main):002:0> “”=~r
=> 0
irb(main):003:0> "
=> 0
irb(main):004:0> “”=~r
=> 0
irb(main):005:0> “”=~r
=> nil
irb(main):006:0> “”=~r
=> 0
irb(main):007:0> "
=> 0
irb(main):008:0> “http://gogo…fr”=~r
=> nil
irb(main):009:0> ""=~r
=> nil

Have a good day,

hi eric,
thanks for help

hi eric,

  one issue
  when i try

irb just hangs and cpu usage on windows xp goes 100%

when i removed that # in the last part of url it gives the result

wonder why it hangs if not of proper format as given it should just


yes, indeed there are problems with Regexp engine. In meanwhile, you can try this regular expression:

a = ""

p $& if a=~ /(^http?:\/{2})\S+\.(\w+)(\S+)$/

#=> ""

p $1 if a=~ /(^http?:\/{2})\S+\.(\w+)(\S+)$/

#=> "http://"

p $3 if a=~ /(^http?:\/{2})\S+\.(\w+)(\S+)$/


Yes, in above regular expression doesn't serve your purpose reply back with what exactly you what?

thanks for help
that will do

You’re welcome!

Sorry for my RegExp, there might be too much recursion for the Regexp engine…
Take care with this one (^http?:/{2})\S+.(\w+)(\S+)$ though :