sre_constants.error: bogus escape
Jeff Epler
jepler at unpythonic.net
Tue Nov 5 14:59:45 EST 2002
On Tue, Nov 05, 2002 at 01:25:19PM -0600, Bradley D. Larson wrote:
> What is the best method of changing a \ character to something else
>
> I'm trying to use re....
>
> pat = re.compile('\\'); ts = pat.sub(':',ts)
>
> which produces....
>
> File "C:\PYTHON21\Lib\sre.py", line 90, in compile
> return _compile(pattern, flags)
> File "C:\PYTHON21\Lib\sre.py", line 136, in _compile
> raise error, v # invalid expression
> sre_constants.error: bogus escape
>
>
It is bogus. If you want to match a literal backslash, the regular
expression '\\\\', also writable as r'\\' is what you're looking for.
'\\' (a 1-character string containing a single backslash) is not a valid
regular expression, much as '%' (a 1-character string containing a single
percent sign) is not a valid format string (i.e., don't try to do "%" %
(...))
Jeff
More information about the Python-list
mailing list