Project

General

Profile

Feature #14669

Regexp does not expose the amount of capture groups.

Added by xfbs (Patrick Elsen) 12 days ago. Updated 11 days ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:86474]

Description

For a project we needed to know how many capture groups a Regex exposes (before actually matching it). The Onigmo regex library used by Ruby has this information, accessible with onig_number_of_captures(const regex_t *), but the Ruby Regexp class doesn't expose this information.

History

#1 [ruby-core:86476] Updated by shevegen (Robert A. Heiler) 12 days ago

Should be added; may have just been forgotten if Onigmo already
supports that.

#2 [ruby-core:86477] Updated by duerst (Martin Dürst) 11 days ago

Can you give a few examples of how this may be used, and explain what's the purpose of getting the number of capture groups before actual matching?

Also, can you show an example what you think the actual interface (e.g. method name) would look like?

On top of that, I think not all Ruby implementations use Onigmo. Some other implementations may not have this feature.

#3 [ruby-core:86478] Updated by Eregon (Benoit Daloze) 11 days ago

This information is available for named captures via Regexp#names and Regexp#named_captures, but it doesn't seem available for unnamed capture groups:

[15] pry(main)> /(?<one>a)(?<two>b)(?<three>c)/.named_captures
=> {"one"=>[1], "two"=>[2], "three"=>[3]}
[16] pry(main)> /(a)(b)(c)/.named_captures
=> {}
[17] pry(main)> /(?<one>a)(?<two>b)(?<three>c)/.names
=> ["one", "two", "three"]
[18] pry(main)> /(a)(b)(c)/.names
=> []

Also available in: Atom PDF