0
why i cannot do pattern = r"(.+) /2" in "word word word"?
it gives error. what it means /2, or /3?
4 ответов
+ 1
\2 means "the same as the second group".
Your expression defines only one group, therefore you cannot use \2.
Ps: beware the syntax you have to use \ not / !
+ 1
So, for completeness to answer the question, you want to do r"(.+?) \1 \1". Notice the ? after it so as to use a "lazy" match and stop at the first space. Also if you only want letters, try r"([a-zA-Z]+?) \1 \1". This would match a character set of only letters. It's generally best practice to be as specific in what you're after as possible, and only use . if you really are okay with anything except a newline
0
oh, thank you:) now it is clear:)
0
try \2