rubyregexbackreferencenamed-captures

What does the "+0" mean in the regexp \k<name+0>?


I'm new to Regular Expressions in Ruby, and I can't seem to find any solid documentation on what \k<name+0> means. It's the +0 part that I'm not getting.

Here's an example - this Regexp matches palindromes:

\A(?<p>(?:(?<l>\w)\g<p>\k<l+0>|\w))\z

When I remove the +0 in \k<l+0> it no longer matches correctly.
My tests:

>> /\A(?<p>(?:(?<l>\w)\g<p>\k<l+0>|\w))\z/.match "aabbcdcbbaa" 
#=> #<MatchData "aabbcdcbbaa" p:"aabbcdcbbaa" l:"c">

>> /\A(?<p>(?:(?<l>\w)\g<p>\k<l>|\w))\z/.match "aabbcdcbbaa" 
#=> nil

All I've done is remove the +0. I haven't yet found any documentation or example of this, can anyone point me in the right direction?


Solution

  • The \k<l+0> works together with the (?<l>\w)

    The match of (?<l>\w) is stored in the capturing group named 'l'

    \k<l+0> Matches the same text that was matched by the named capturing group 'l' when it was at the same recursion level as this backreference is now