I''m looking to write a regular expression that will match valid URLs.
My problem is that it almost works, except it accepts URLs with / in the
middle of them, suchs as:
http://www.ruby/rails.com
It looks (to me) like my regular expression should not match strings
like that, but it does. Here is the regular expression:
.match(/^((http|https):\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.(\w+).*$/ix)
How can I make it not accept URLs with / in the middle of them?
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Google''s your friend here. "validate url with regular
expression".
-----Original Message-----
From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
[mailto:rubyonrails-talk@googlegroups.com] On Behalf Of Joe Peck
Sent: Wednesday, October 15, 2008 7:45 AM
To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
Subject: [Rails] Small regular expression question
I''m looking to write a regular expression that will match valid URLs.
My problem is that it almost works, except it accepts URLs with / in the middle
of them, suchs as:
http://www.ruby/rails.com
It looks (to me) like my regular expression should not match strings like that,
but it does. Here is the regular expression:
.match(/^((http|https):\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.(\w+).*$/ix)
How can I make it not accept URLs with / in the middle of them?
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
On 15 Oct 2008, at 15:44, Joe Peck wrote:> > I''m looking to write a regular expression that will match valid URLs. > My problem is that it almost works, except it accepts URLs with / in > the > middle of them, suchs as: > http://www.ruby/rails.com > > It looks (to me) like my regular expression should not match strings > like that, but it does. Here is the regular expression: > > .match(/^((http|https):\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.(\w+).* > $/ix) > > How can I make it not accept URLs with / in the middle of them?for the record, the problem here is .* at the end ^((http|https):\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.(\w+) matches against http://www.ruby and the remainder (/rails.com) trivially matches against .* Fred> > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 15 Oct 2008, at 15:44, Joe Peck wrote: > >> .match(/^((http|https):\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.(\w+).* >> $/ix) >> >> How can I make it not accept URLs with / in the middle of them? > > for the record, the problem here is .* at the end > ^((http|https):\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.(\w+) > matches against > http://www.ruby > and the remainder (/rails.com) trivially matches against .* > > FredThanks, I don''t know how I missed that. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I''d check out the ruby URI module: http://www.ruby-doc.org/core/classes/URI.html It''s a more complex problem than it appears at first glance. good luck! On Oct 15, 7:44 am, Joe Peck <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I''m looking to write a regular expression that will match valid URLs. > My problem is that it almost works, except it accepts URLs with / in the > middle of them, suchs as:http://www.ruby/rails.com > > It looks (to me) like my regular expression should not match strings > like that, but it does. Here is the regular expression: > > .match(/^((http|https):\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.(\w+).*$/ix) > > How can I make it not accept URLs with / in the middle of them? > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---