# Regexp Question - Merb::Router

**URL:** <https://www.ruby-forum.com/t/regexp-question-merb-router/109302>\
**Category:** Ruby\
**Created:** [July 13, 2007, 9:38am UTC](https://www.ruby-forum.com/t/regexp-question-merb-router/109302 "2007-07-13T09:38:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [July 13, 2007, 9:38am UTC](https://www.ruby-forum.com/t/regexp-question-merb-router/109302/1 "2007-07-13T09:38:25Z")

</div>

Hi,

I’m looking through the Merb::Routing code and I’ve found a regexp that  
I  
can’t figure out how it works.

Merb::Router::SECTION\_REGEXP #=\> /(?::([a-z\*\_]+))/

It takes a route definition string, like “/products/:model/:id” and  
extracts the “model” string on the first pass, and later the “id”  
string.

Can anyone shed some light on what the  
?::  
part does? I haven’t found it in any of the documentation for Ruby  
Regexps  
that I’ve found.

Cheers  
Daniel

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [July 13, 2007, 9:51am UTC](https://www.ruby-forum.com/t/regexp-question-merb-router/109302/2 "2007-07-13T09:51:27Z")

</div>

2007/7/13, Daniel N [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Can anyone shed some light on what the  
> ?::  
> part does? I haven’t found it in any of the documentation for Ruby Regexps  
> that I’ve found.

It’s actually _two_ parts: (?🙂 is a non capturing regexp group. The  
second “:” literally matches a single colon.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [July 13, 2007, 10:13am UTC](https://www.ruby-forum.com/t/regexp-question-merb-router/109302/3 "2007-07-13T10:13:29Z")

</div>

On 7/13/07, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > It takes a route definition string, like “/products/:model/:id” and  
> > second “:” literally matches a single colon.
> 
> Kind regards
> 
> robert
> 
> Thanx Robert,

I’ve not heard of that before. Makes searching on google much easier 🙂

/(?::([a-z\*\_]+))/

I guess this means that a nested group within this non capturing group  
(this  
case) is captured though since the $1 variable is refered to later.

Cheers  
Daniel

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [July 13, 2007, 10:23am UTC](https://www.ruby-forum.com/t/regexp-question-merb-router/109302/4 "2007-07-13T10:23:31Z")

</div>

2007/7/13, Daniel N [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > 
> > 
> > It’s actually _two_ parts: (?🙂 is a non capturing regexp group. The  
> > /(?::([a-z\*\_]+))/
> 
> I guess this means that a nested group within this non capturing group (this  
> case) is captured though since the $1 variable is refered to later.

Exactly. Btw, in your case I’d probably use another regexp. Some  
samples:

irb(main):001:0\> “/products/:model/:id”.scan %r{/:([^/]+)}  
=\> [[“model”], [“id”]]  
irb(main):002:0\> “/products/:model/:id”.scan %r{:([^/]+)}  
=\> [[“model”], [“id”]]  
irb(main):004:0\> “/products/:model/:id”.scan %r{:[^/:]+}  
=\> [“:model”, “:id”]

But of course I do not know the full spec of your input data.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [July 13, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/regexp-question-merb-router/109302/5 "2007-07-13T16:28:45Z")

</div>

On Jul 13, 2007, at 3:12 AM, Daniel N wrote:

> /(?::([a-z\*\_]+))/
> 
> I guess this means that a nested group within this non capturing  
> group (this  
> case) is captured though since the $1 variable is refered to later.

Yes. The part without the colon is capture, because of the normal  
parenthesis.

Unless this expression is later embedded in another expression, the  
non-capturing group does nothing here and could safely be removed.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [July 13, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/regexp-question-merb-router/109302/6 "2007-07-13T16:47:48Z")

</div>

2007/7/13, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Unless this expression is later embedded in another expression, the  
> non-capturing group does nothing here and could safely be removed.

Even in that case the grouping can be removed:

irb(main):002:0\> /foo/.to\_s  
=\> “(?-mix:foo)”  
irb(main):003:0\> /(?:foo)/.to\_s  
=\> “(?-mix:foo)”

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![liquid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/47e85d/32.png) [@liquid](https://www.ruby-forum.com/u/liquid)\
**Post date:** [July 13, 2007, 10:34am UTC](https://www.ruby-forum.com/t/regexp-question-merb-router/109302/7 "2007-07-13T10:34:38Z")

</div>

On 7/13/07, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > can’t figure out how it works.  
> > > > Regexps
> 
> irb(main):001:0\> “/products/:model/:id”.scan %r{/:([^/]+)}  
> robert

I’m just trying to understand that regexp’s place in the Merb routing  
code  
at the moment.

You’ve cleared it up nicely for me.

Thankyou  
Daniel
