backslash in regular expression problem

Gordon McLeod Gordon.McLeod at wolfson.co.uk
Fri Jun 16 12:28:42 EDT 2000


Apologies if this has been asked before -  I'm a newbie. I am trying get
rid of backslashes from a string using re.sub in python 1.5.2:

import re
string="\END\\"
x=re.sub('[\]','',string)
print x

and expect x to be 'END' but I get:

Traceback (innermost last):
  File "<interactive input>", line 1, in ?
  File "C:\Program Files\Python\Lib\re.py", line 47, in sub
    pattern = _cachecompile(pattern)
  File "C:\Program Files\Python\Lib\re.py", line 33, in _cachecompile
    value = compile(pattern, flags)
  File "C:\Program Files\Python\Lib\re.py", line 79, in compile
    code=pcre_compile(pattern, flags, groupindex)
pcre.error: ('missing terminating ] for character class', 3)

I am trying to follow the advice of the Python Library Reference Manual,
section 4.3 which says:

     However, if you want to include a literal backslash in a
     regular expression represented as a string literal, you have
     to quadruple it or enclose it in a singleton character class.
     E.g. to extract L A T E X `\section{. . . }' headers from a
     document, you can use this pattern: '[\]section{\(.*\)}'.

It looks like Python sees the [\]  as [\\]. [\_] doesn't crash but
doesn't sub either.  Can anyone help?

Thanks,

Gordon McLeod,




More information about the Python-list mailing list