Static group references in recursive pattern (Ruby 1.9)

  • Thread starter Wolfgang Nádasi-Donner
  • Start date
W

Wolfgang Nádasi-Donner

I've recognized an additional problem, which might come from wrong
expectations.

A short program:

palindrome = [ "rr",
"rer",
"reer"]

palindrome.each do |text|
if (md = text.match(/^(?<o>|.|(?:(?<i>.)\g<o>\k<i>))$/))
puts "'#{text}' is a palindrome"
puts md[1]
puts md[2]
else
puts "no match for '#{text}'"
end
end

Results in:

'rr' is a palindrome
rr
r
'rer' is a palindrome
rer
r
no match for 'reer'

I'm surprised that named groups, that are defined inside a named group a
like "global references". Is there a possibility to change this behaviour,
that "\k<i>" refers to the associated "(?<i>.)" in the actual recursive call
(like "local variables").

Best regards, Wolfgang Nadasi-Donner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top