>>>>> "L" == Leonid Razoumov <see_signature / 127.0.0.1> writes:

L> Certainly it is a problem.

 I'll try to explain the rules used by ruby.

 With this regexp /(\2(a\1))/

  * at compile time

 \2 is resolved as the character \002 because 2 is greater than the number
 of '(' at this time.

 \1 is resolved as the first sub-group () (1 is less than the number of
 '('), i.e. the comment in the source is wrong.

  * at runtime, ruby use a very strange rule when it try to match \1

    \1 fail if
      - it make reference to a *valid* sub-group which is *closed*
      - the correspond string is not found

    in *all* other cases it succeed.


 This mean that in reality the regexp /(\2(a\1))/ is interpreted by ruby as 
 /(\002(a))/


 /(a)*-\1b/ succeed with 'aaa-b' because in its last case (i.e. 0
 occurences of 'a') the sub-group is *not* valid ==> success

pigeon% ruby -e 'puts "OK #$&" if /(a)*-\1b/ =~ "aaa-b"'
OK -b
pigeon% 

 Same reason for /(a)|\1/ with "b"



Guy Decoux