[newbie] Strange behavior of the re module

Tim Peters tim.peters at gmail.com
Sat Aug 21 07:13:20 CEST 2004


[Fred <fred at acme.com>]
> import re
> 
> #NOK
> stuff=r"\colortbl\red0\gn0"
> #NOK
> stuff=R"\colortbl\red0\gn0"
> 
> template = "BLA"
> template = re.sub('BLA', stuff, template)
> ---------------------------------------
> 
> Traceback (most recent call last):
>  File "C:\test.py", line 9, in ?
>    template = re.sub('BLA', stuff, template)
>  File "G:\Python23\lib\sre.py", line 143, in sub
>    return _compile(pattern, 0).sub(repl, string, count)
>  File "G:\Python23\lib\sre.py", line 257, in _subx
>    template = _compile_repl(template, pattern)
>  File "G:\Python23\lib\sre.py", line 244, in _compile_repl
>    raise error, v # invalid expression
> sre_constants.error: bad group name

I can't figure out what you're trying to accomplish here, but the
error msg makes sense.  You should pause to read the docs for re.sub. 
In

    re.sub('BLA', stuff, template)

'BLA' is the regular expression, stuff is the substitution pattern,
and template is the input string.  As the docs say, \g in the
substitution pattern has special meaning, specifying the name of a
named capturing group.  Your regular expression ('BLA') has no
capturing groups (let alone named ones), so using \g in the
substitution pattern can't work.

If you really want to search for the regular expression 'BLA' in
template and replace each occurence with the string

    r"\colortbl\red0\gn0"

then you need to escape all characters with special meaning in the
substitution pattern, via re.escape():

>>> stuff=r"\colortbl\red0\gn0"
>>> template = "BLA"
>>> print re.sub('BLA', re.escape(stuff), template)
\colortbl\red0\gn0
>>>



More information about the Python-list mailing list