From: "Wolfgang Nádasi-Donner" Date: 2009-02-25T01:13:43+09:00 Subject: [ruby-core:22415] [Feature #1200] Possibility for using named and normal groups together in regular expressions Feature #1200: Possibility for using named and normal groups together in regular expressions http://redmine.ruby-lang.org/issues/show/1200 Author: Wolfgang N��dasi-Donner Status: Open, Priority: Normal Category: core, Target version: 1.9.1 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 http://www.ruby-mine.de/2009/2/7/regul%C3%A4re-ausdr%C3%BCcke-teil-7-oniguruma-und-statische-relativbez%C3%BCge): # encoding: Windows-1252 module Matchelements def bal() return "(" + "[^()]*?" + "(?:\\(\\g<-1>\\)" + "[^()]*?" + ")*?" + ")" end end 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}'" else puts "Nicht O.K.: '#{s}'" end end 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. ---------------------------------------- http://redmine.ruby-lang.org