vendredi 8 juillet 2016

re.sub tries to escape repl string?

So this doesn't work with python's regex:

>>> re.sub('oof', 'bar\', 'foooof')

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:Python27libre.py", line 151, in sub
    return _compile(pattern, flags).sub(repl, string, count)
  File "C:Python27libre.py", line 270, in _subx
    template = _compile_repl(template, pattern)
  File "C:Python27libre.py", line 257, in _compile_repl
    raise error, v # invalid expression
error: bogus escape (end of line)

I thought my eyes were deceiving me, so I did this:

>>> re.sub('oof', "barx5c", 'foooof')

Got the same thing. I've searched and have confirmed people have this problem. So what's the problem with treating repl as just an ordinary string? Are there additional formatting options that can be in placed in repl?

Aucun commentaire:

Enregistrer un commentaire