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.

regards
gaurav

Hi!

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> “www.google.com”=~r
=> 0
irb(main):003:0> “http://www.google.fr”=~r
=> 0
irb(main):004:0> “https://www.google.de”=~r
=> 0
irb(main):005:0> “http://.google.de”=~r
=> nil
irb(main):006:0> “class Regexp - RDoc Documentation”=~r
=> 0
irb(main):007:0> "
ruby - Recherche Google"=~r
=> 0
irb(main):008:0> “http://gogo…fr”=~r
=> nil
irb(main):009:0> “http://gogol.de.”=~r
=> nil

Have a good day,
Eric

hi eric,
thanks for help
regards
gaurav

hi eric,

one issue
when i try
irb(main):004:0>
http://groups-beta.google.com/group/rubyonrails-talk/browse_th
read/thread/8f085b191387d799/e78a71cbd7354c0c#e78a71cbd7354c0c”=~r

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
fail.

regards
gaurav

hi,
thanks for help
that will do
~gaurav

[email protected] wrote:

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
fail.

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

a =
http://groups-beta.google.com/group/rubyonrails-talk/browse_thread/thread/8f085b191387d799/e78a71cbd7354c0c#e78a71cbd7354c0c

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

#=>
http://groups-beta.google.com/group/rubyonrails-talk/browse_thread/thread/8f085b191387d799/e78a71cbd7354c0c#e78a71cbd7354c0c

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

#=> “http://”

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

#=>“/group/rubyonrails-talk/browse_thread/thread/8f085b191387d799/e78a71cbd7354c0c#e78a71cbd7354c0c”

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

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 :

r=/(^http?:/{2})\S+.(\w+)(\S+)$/
‘htt://==.f-é#-[==/’=~r
=>0