Forum: Ruby on Rails validating URL with regular expression

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cd1de7e6a1ff7903b249edc2a4a35b34?d=identicon&s=25 gaurav bagga (Guest)
on 2006-12-25 11:27
(Received via mailing list)
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
819c93493548c6d70becee01363b4bfd?d=identicon&s=25 Eric DUMINIL (Guest)
on 2006-12-26 19:36
(Received via mailing list)
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> "http://www.ruby-doc.org/core/classes/Regexp.html"=~r
=> 0
irb(main):007:0> "
http://www.google.fr/search?hl=fr&q=ruby&btnG=Rech...
=> 0
irb(main):008:0> "http://gogo..fr"=~r
=> nil
irb(main):009:0> "http://gogol.de."=~r
=> nil

Have a good day,
Eric
Cd1de7e6a1ff7903b249edc2a4a35b34?d=identicon&s=25 unknown (Guest)
on 2006-12-27 06:07
(Received via mailing list)
hi eric,
 thanks for help
regards
gaurav
Cd1de7e6a1ff7903b249edc2a4a35b34?d=identicon&s=25 unknown (Guest)
on 2006-12-27 06:17
(Received via mailing list)
hi eric,

  one issue
  when i try
  irb(main):004:0>
"http://groups-beta.google.com/group/rubyonrails-ta...
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
87e41d0d468ad56a3b07d9a6482fd6d5?d=identicon&s=25 Hemant Kumar (gnufied)
on 2006-12-27 11:07
(Received via mailing list)
gaurav.v.bagga@gmail.com 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-ta...

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

 #=>
"http://groups-beta.google.com/group/rubyonrails-ta...

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?
Cd1de7e6a1ff7903b249edc2a4a35b34?d=identicon&s=25 unknown (Guest)
on 2006-12-28 06:19
(Received via mailing list)
hi,
thanks for help
that will do
~gaurav
819c93493548c6d70becee01363b4bfd?d=identicon&s=25 Eric DUMINIL (Guest)
on 2006-12-28 22:41
(Received via mailing list)
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
This topic is locked and can not be replied to.