Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Regexp Arity

Reply
Thread Tools

Regexp Arity

 
 
Florian Frank
Guest
Posts: n/a
 
      09-23-2004
On Thu, 2004-09-23 at 02:03, Nikolai Weibull wrote:
> * Gavin Sinclair <(E-Mail Removed)> [Sep 23, 2004 01:50]:
> > > Regexp#groups ?
> > > Regexp#groupcount?

> >
> > Regexp#ngroups

>
> Best by far, so far,


Which groups are counted here? There are capturing groups and
non-capturing groups. Or is this the sum of those two numbers?

At the moment I don't see any obvious reason to count those
groups in a Regexp at all. I should know everything about them
before I created the regexp. If I want to find out how many
captures I got after matching the regexp I can easily get that
info from MatchData#captures.size.

On the other hand the grouping is a tree structure and if I simply
compute one number from a regexp I loose a lot of information. They can
be quite complicated like /(((a))(?b|c)|d))/. From the group
size alone I cannot infer very much about the values that will be
captured after a match. Simons tree construction example makes
much more sense if one wants to examine the structure of
a regexp.

--
Florian Frank <(E-Mail Removed)>
Private Linux Site



 
Reply With Quote
 
 
 
 
Nikolai Weibull
Guest
Posts: n/a
 
      09-23-2004
* Florian Frank <(E-Mail Removed)> [Sep 23, 2004 03:00]:
> > > > Regexp#groups ?
> > > > Regexp#groupcount?


> > > Regexp#ngroups


> > Best by far, so far,


> Which groups are counted here? There are capturing groups and
> non-capturing groups. Or is this the sum of those two numbers?


Precisely why it isn't great.

> At the moment I don't see any obvious reason to count those
> groups in a Regexp at all. I should know everything about them
> before I created the regexp. If I want to find out how many
> captures I got after matching the regexp I can easily get that
> info from MatchData#captures.size.


> On the other hand the grouping is a tree structure and if I simply
> compute one number from a regexp I loose a lot of information. They can
> be quite complicated like /(((a))(?b|c)|d))/. From the group
> size alone I cannot infer very much about the values that will be
> captured after a match. Simons tree construction example makes
> much more sense if one wants to examine the structure of
> a regexp.


The only viable thing to do is return exactly the number of capturing
groups given, not excluding anything. If you wish to write a regex such
as yours above, then you are in it for the trouble. I agree with you,
however, that designing based on the knowledge of how many capturing
groups are in a regex isn't very good design at all.
nikolai


--
::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      09-23-2004
Hi --

On Thu, 23 Sep 2004, Florian Frank wrote:

> On Thu, 2004-09-23 at 02:03, Nikolai Weibull wrote:
> > * Gavin Sinclair <(E-Mail Removed)> [Sep 23, 2004 01:50]:
> > > > Regexp#groups ?
> > > > Regexp#groupcount?
> > >
> > > Regexp#ngroups

> >
> > Best by far, so far,

>
> Which groups are counted here? There are capturing groups and
> non-capturing groups. Or is this the sum of those two numbers?
>
> At the moment I don't see any obvious reason to count those
> groups in a Regexp at all. I should know everything about them
> before I created the regexp. If I want to find out how many
> captures I got after matching the regexp I can easily get that
> info from MatchData#captures.size.


I agree. It's sounding like this is being informally proposed as a
new core method (rather than just discussed as something one might
write ad hoc), and I haven't seen a case being made for it at all.


David

--
David A. Black
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
trans. (T. Onoma)
Guest
Posts: n/a
 
      09-23-2004
On Wednesday 22 September 2004 09:13 pm, David A. Black wrote:

> I agree. It's sounding like this is being informally proposed as a
> new core method (rather than just discussed as something one might
> write ad hoc), and I haven't seen a case being made for it at all.


Well, you may be right. Most scenarios can be adjusted for *after the match*.
The only *necessary* use case (since that's what you'r after) would come from
something that could only be done before matching happens. Given just how
dynamic Ruby is, that something of course will be hard to find. (BTW --Ruby's
dynamic method arguemts *args is exactly how I resolved my potential use
case.)

On the other hand, if this information is already "near the surface" in the
Regexp engine. It certainly shouldn't be a big deal to add a method to access
it. It's funny how people are more likely to find uses for things when they
can actually use them. Any way, it's no big deal. I was just wondering if
anyone else thought they might be of use.

FYI --Concerning which type of captures to count, only groups that can
actually capture are of any external use. So only they need counting really.

--
( o _ カラチ
// trans.
/ \ (E-Mail Removed)

I don't give a damn for a man that can only spell a word one way.
-Mark Twain



 
Reply With Quote
 
trans. (T. Onoma)
Guest
Posts: n/a
 
      09-23-2004
On Wednesday 22 September 2004 09:13 pm, David A. Black wrote:
> > I agree. It's sounding like this is being informally proposed as a
> > new core method (rather than just discussed as something one might
> > write ad hoc), and I haven't seen a case being made for it at all.


BTW --What's the use case of #casefold?

T.


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      09-23-2004

"trans. (T. Onoma)" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> On Wednesday 22 September 2004 09:13 pm, David A. Black wrote:
> > > I agree. It's sounding like this is being informally proposed as a
> > > new core method (rather than just discussed as something one might
> > > write ad hoc), and I haven't seen a case being made for it at all.

>
> BTW --What's the use case of #casefold?
>
> T.
>
>

You can query whether the regexp at hand ignores case or not. (That's the
/i flag)

robert

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why the expression "(1)" is not an one-arity tuple, but int ? Python 0 12-04-2009 12:17 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Detecting the arity of a constructor at compile time Michael Feathers C++ 3 08-04-2006 04:18 PM
overloading on the template parameter arity of a template templateparameter Howard Gardner C++ 4 07-19-2006 11:10 PM
Regexp arity revisited trans. (T. Onoma) Ruby 0 10-20-2004 01:52 AM



Advertisments