Static group references in recursive pattern (Ruby 1.9)

Discussion in 'Ruby' started by Wolfgang Nádasi-Donner, Jun 29, 2006.

  1. 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
     
    Wolfgang Nádasi-Donner, Jun 29, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. peter
    Replies:
    9
    Views:
    307
    Michael Spencer
    Feb 17, 2005
  2. n00m
    Replies:
    12
    Views:
    1,122
  3. mark starnes
    Replies:
    1
    Views:
    286
    George Sakkis
    Nov 10, 2008
  4. vamsi
    Replies:
    21
    Views:
    2,114
    Keith Thompson
    Mar 9, 2009
  5. Sci000 Lem.

    recursive method and references

    Sci000 Lem., Jul 20, 2006, in forum: Ruby
    Replies:
    2
    Views:
    134
    Sci000 Lem.
    Jul 21, 2006
Loading...

Share This Page