+ 3

Can anyone explain me how r"(.+) \1" expression exactly works?

5th Aug 2016, 6:16 AM
Ratnadeep Dey
Ratnadeep Dey - avatar
7 Respostas
+ 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
6th Aug 2016, 12:23 AM
Tom Post
Tom Post - avatar
+ 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
8th Aug 2016, 7:37 AM
Tom Post
Tom Post - avatar
+ 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????
8th Aug 2016, 6:58 AM
Ratnadeep Dey
Ratnadeep Dey - avatar
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!
30th Aug 2016, 4:09 PM
Jason G
0
\1 means the first group again. So if you have only one capturing group and you put \2 then an error will appear
13th Jun 2018, 3:42 AM
Cristian Baeza Jimenez
Cristian Baeza Jimenez - avatar
0
If you want to consider spaces then the patern will be: (.+)(\s)?\1
13th Jun 2018, 3:45 AM
Cristian Baeza Jimenez
Cristian Baeza Jimenez - avatar
- 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???
8th Aug 2016, 7:54 AM
Ratnadeep Dey
Ratnadeep Dey - avatar