As of my understanding \2 represents the contents of group 2.
So, the expression r'(\w*)(\w)\2'
should return the contents of group2( i.e \w
)
but when we use the word having repeated characters it is returning repeated characters.
example:
re.search(r'(\w*)(\w)\2','finally').group(2) -> 'l'
re.search(r'(\w*)(\w)\2','finallyy').group(2) ->'y'
In the 1st example the output is 'l' instead of 'y'.
Can anyone tell me what exactly \2 mean in regular expression and where does my understanding is wrong.