Project

General

Profile

Actions

Feature #1200

closed

Possibility for using named and normal groups together in regular expressions

Added by WoNaDo (Wolfgang Nádasi-Donner) about 15 years ago. Updated about 12 years ago.

Status:
Rejected
Assignee:
-
Target version:
[ruby-core:22415]

Description

=begin
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.
=end


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

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0