Forum: Ruby Regexp Question - Merb::Router

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.
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2007-07-13 09:38
(Received via mailing list)
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-07-13 09:51
(Received via mailing list)
2007/7/13, Daniel N <has.sox@gmail.com>:
> 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
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2007-07-13 10:13
(Received via mailing list)
On 7/13/07, Robert Klemme <shortcutter@googlemail.com> 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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-07-13 10:23
(Received via mailing list)
2007/7/13, Daniel N <has.sox@gmail.com>:
> > >
> > 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
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2007-07-13 10:34
(Received via mailing list)
On 7/13/07, Robert Klemme <shortcutter@googlemail.com> 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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-07-13 16:28
(Received via mailing list)
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 Gray II
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-07-13 16:47
(Received via mailing list)
2007/7/13, James Edward Gray II <james@grayproductions.net>:
>
> 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
This topic is locked and can not be replied to.