# Small regular expression question

**URL:** <https://www.ruby-forum.com/t/small-regular-expression-question/150987>\
**Category:** Rails\
**Created:** [October 15, 2008, 4:44pm UTC](https://www.ruby-forum.com/t/small-regular-expression-question/150987 "2008-10-15T16:44:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [October 15, 2008, 4:44pm UTC](https://www.ruby-forum.com/t/small-regular-expression-question/150987/1 "2008-10-15T16:44:48Z")

</div>

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](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?

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [October 15, 2008, 6:15pm UTC](https://www.ruby-forum.com/t/small-regular-expression-question/150987/2 "2008-10-15T18:15:41Z")

</div>

Google’s your friend here. “validate url with regular expression”.

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [October 15, 2008, 7:49pm UTC](https://www.ruby-forum.com/t/small-regular-expression-question/150987/3 "2008-10-15T19:49:43Z")

</div>

On 15 Oct 2008, at 15:44, Joe P. 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](http://www.ruby)  
and the remainder (/rails.com) trivially matches against ._

Fred

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [October 15, 2008, 7:55pm UTC](https://www.ruby-forum.com/t/small-regular-expression-question/150987/4 "2008-10-15T19:55:12Z")

</div>

Frederick C. wrote:

> On 15 Oct 2008, at 15:44, Joe P. 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](http://www.ruby)  
> and the remainder (/rails.com) trivially matches against ._
> 
> Fred

Thanks, I don’t know how I missed that.

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [October 15, 2008, 8:03pm UTC](https://www.ruby-forum.com/t/small-regular-expression-question/150987/5 "2008-10-15T20:03:14Z")

</div>

I’d check out the ruby URI module:

[http://www.ruby-doc.org/core/classes/URI.html](http://www.ruby-doc.org/core/classes/URI.html)

It’s a more complex problem than it appears at first glance.

good luck!
