Reputation: 786
I've been using this regex to bring up doubled words in some text:
pattern = re.compile(" ([a-zA-Z]+) \1 ")
result = re.search(pattern, someStringFromAFile)
Using it in grep and Notepad++, it detects all the things I want it to, like " at at " and " ninja ninja ".
However, when I try to match the same text with Python regex, it always comes up None, meaning it didn't see the match. I'd like to know how to modify what I'm doing in Python to make it work.
If additionally you can explain why Python isn't doing what Notepad++ and grep are doing, that would be awesome too :) Thanks!
Upvotes: 0
Views: 71
Reputation: 523534
Because \1
means the character with value 1 in normal strings. Use r"..."
for raw string to keep backslash meaning a backslash.
pattern = re.compile(r" ([a-zA-Z]+) \1 ")
Upvotes: 8