Re: Small regular expression question

Frederick Cheung
Wed, 15 Oct 2008 17:49:06 -0000

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