Feature #1200


Possibility for using named and normal groups together in regular expressions

Added by WoNaDo (Wolfgang Nádasi-Donner) almost 14 years ago. Updated almost 11 years ago.

Target version:


It should be possible to use named and normal groups together in ane regular expression.

Reason: The new relative adressing possibilities for groups, \k<-n>, \k'-n', \g<-n>, and \g'-n' are very helpful for writing regular subexpresions to be used via #{...} more than once in a regular expresion. Example (longer explanations are only available in German on

encoding: Windows-1252

module Matchelements
def bal()
return "(" +
"[^()]?" +
"(?:\(\g<-1>\)" +
?" +
")*?" +
include Matchelements

orgstrings= [
'firstproc(x1(33, r(3, 4)), k(3, kk(3, 4)), l(3), x2(99))', # (x1, ., ., x2)
'secondproc(x1(99,5), l(77, m( n(44), 29)), x2(15))', # (x1, ., x2)
'thirdproc(x1(66), x2(88))', # (x1, x2)
'fourthproc(x1(44), 1, 2, 3, x2(234))' # (x1, ., ., ., x2)

pattern = /\w+(x1(#{bal}),(?>#{bal},){1,2} x2(#{bal})/
orgstrings.each do |s|
if s.match(pattern)
puts " O.K.: '#{s}'"
puts "Nicht O.K.: '#{s}'"

This works fine:

ruby191-p0 balmusterWorks.rb
O.K.: 'firstproc(x1(33, r(3, 4)), k(3, kk(3, 4)), l(3), x2(99))'
O.K.: 'secondproc(x1(99,5), l(77, m( n(44), 29)), x2(15))'
Nicht O.K.: 'thirdproc(x1(66), x2(88))'
Nicht O.K.: 'fourthproc(x1(44), 1, 2, 3, x2(234))'

One Problem is still open, because in the regular expression, that uses the subexpresions, their groups still count. If one wants to extract parts of a match normal groups are necessary, which numbers must be known - e.g. /#{group}([0-9}+)#{group}/.

In this case the usage of the result of ([0-9}+) is only possible, if one knows the number of the group. This is not visible from /#{group}([0-9}+)#{group}/, because the number of groups used in #{group} can only be seen by looking at the definition, which can be somewhere.

A good solution is the usage of a named group /#{group}(?[0-9}+)#{group}/, but then it is no longer possible to use normal groups together with relative access in the definition of regular subexpresions.

It would be very helpul to allow both in one regular expression.

Related issues 2 (0 open2 closed)

Related to Ruby master - Feature #1201: Add relative group reference in back reference with nest level in regular expressions (e.g. \k<-5+1>)Closedmatz (Yukihiro Matsumoto)02/25/2009Actions
Related to Ruby 1.8 - Feature #4239: Let's begin a talk for "1.8.8" -- How's needed for surviving 1.8?Closedshyouhei (Shyouhei Urabe)01/06/2011Actions
Actions #1

Updated by WoNaDo (Wolfgang Nádasi-Donner) almost 14 years ago

Typo: I've written "[0-9}+" instead of "[0-9]+" several times, sorry.

Actions #2

Updated by akr (Akira Tanaka) over 13 years ago

I'd like to use simple paren as a shy group.

If we allow both named and unnamed capturing group in a regexp, I recommend unnamed capturing have a new syntax, such as (?<>...).


Actions #3

Updated by marcandre (Marc-Andre Lafortune) about 13 years ago

  • Assignee set to matz (Yukihiro Matsumoto)
  • Target version changed from 1.9.1 to 2.0.0



Actions #4

Updated by shyouhei (Shyouhei Urabe) about 12 years ago

  • Status changed from Open to Assigned



Updated by naruse (Yui NARUSE) almost 11 years ago

  • Status changed from Assigned to Rejected
  • Assignee deleted (matz (Yukihiro Matsumoto))

Mixed regexp both named and unnamed capturing group is disallowed by design of Ruby.


Also available in: Atom PDF