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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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=Recherche+Google&meta=test"=~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 On 25/12/06, gaurav bagga <gaurav.v.bagga-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
gaurav.v.bagga-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-27 05:04 UTC
Re: validating URL with regular expression
hi eric, thanks for help regards gaurav --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
gaurav.v.bagga-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-27 05:16 UTC
Re: validating URL with regular expression
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
gaurav.v.bagga-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > 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. >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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
gaurav.v.bagga-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-28 04:41 UTC
Re: validating URL with regular expression
hi, thanks for help that will do ~gaurav On Dec 27, 3:05 pm, Hemant Kumar <gethem...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> gaurav.v.ba...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > > 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.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/th..." > > p $& if a=~ /(^http?:\/{2})\S+\.(\w+)(\S+)$/ > > #=> > "http://groups-beta.google.com/group/rubyonrails-talk/browse_thread/th..." > > 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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On 28/12/06, gaurav.v.bagga-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <gaurav.v.bagga-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > hi, > thanks for help > that will do > ~gaurav > > On Dec 27, 3:05 pm, Hemant Kumar <gethem...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > gaurav.v.ba...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > > > > 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.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/th.. > ." > > > > p $& if a=~ /(^http?:\/{2})\S+\.(\w+)(\S+)$/ > > > > #=> > > "http://groups-beta.google.com/group/rubyonrails-talk/browse_thread/th.. > ." > > > > 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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---