Forum: Ruby on Rails Small regular expression question

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.
2119f016588ba13373484491bd2dd6d1?d=identicon&s=25 Joe Peck (fatcatt316)
on 2008-10-15 16:44
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?
D6434aa0b7b350f8c3ed0119d81b2ead?d=identicon&s=25 Roy Pardee (rpardee)
on 2008-10-15 18:15
(Received via mailing list)
Google's your friend here.  "validate url with regular expression".
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-15 19:49
(Received via mailing list)
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 .*

Fred
2119f016588ba13373484491bd2dd6d1?d=identicon&s=25 Joe Peck (fatcatt316)
on 2008-10-15 19:55
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 .*
>
> Fred

Thanks, I don't know how I missed that.
Ab0b702e7fcc79bb7d2c6c67f64e1b4d?d=identicon&s=25 Tim McIntyre (Guest)
on 2008-10-15 20:03
(Received via mailing list)
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!
This topic is locked and can not be replied to.