Use raw strings:
>>> re.findall("(.+?)\1+", "FFFFFFF")
[]
>>> re.findall(r"(.+?)\1+", "FFFFFFF")
['F']
>>>
Raw string literals, i.e. string literal prefixed with 'r'
, make the backslashes to be treated as literal. Backslashes are otherwise treated as escape sequences.
Quoting from re
— Regular expression operations:
Regular expressions use the backslash character ('\'
) to indicate
special forms or to allow special characters to be used without
invoking their special meaning. ...
The solution is to use Python’s raw string notation for regular
expression patterns; backslashes are not handled in any special way in
a string literal prefixed with 'r'
. So r"\n"
is a two-character string
containing '\'
and 'n'
, while "\n"
is a one-character string
containing a newline. Usually patterns will be expressed in Python
code using this raw string notation.