+ 3
Can anyone explain me how r"(.+) \1" expression exactly works?
7 ответов
+ 12
we begin with a rawstring: r""
then we have a group: r"()"
in that group we have 2 metacharacters: r"(.+)"
+ means one or more repititions of the last things (.)
. means whatever characters.
so it means whatever characters 1 or more times.
\1 means the previous thing followed by the same thing.
putting it all together:
r"(characters)* 2
hope it helps
+ 3
actually, this has to work:
\1 means something followed by 1 time the same thing. like: abc abc.
\n (n representing a number) means something followed by n times the same WITH SPACES IN
BETWEEN
hope it helps
+ 1
Thank you Tom for the explanation however I need one more favour from you can you tell me why is the same pattern not working with \2 or \3 or any other number between 1 to 99 even if I increase the number of repetition of the same character accordingly????
0
I get a match with "wordword", no match with "word word".
Also, \n for n > 1 gives me a "bogus escape" error (yes I'm using a raw string).
This is interactively in IDLE 2.7.6; https://docs.python.org/2/library/re.html says the behaviour is wrong!
0
\1 means the first group again. So if you have only one capturing group and you put \2 then an error will appear
0
If you want to consider spaces then the patern will be: (.+)(\s)?\1
- 1
I tried the same way as you suggested me however it throws an error naming "bogus escape" can you give a try to this pattern and let me know whether it works or not???