trouble with regex with escaped metachars (URGENT please O:-)

anton muhin antonmuhin.REMOVE.ME.FOR.REAL.MAIL at rambler.ru
Thu Nov 20 10:47:49 EST 2003


Fernando Rodriguez wrote:

> Hi,
> 
> I have a filewhose contents looks like this:
> 
> Compression=bzip/9
> OutputBaseFilename=$<OutputFileName>
> OutputDir=$<OutputDir>
> LicenseFile=Z:\apps\easyjob\main\I18N\US\res\license.txt
> 
> The tokens $<...> must be susbtituted by some user-provided string.  The
> problem is that those user-provided strings might contain metacharacters, so I
> escape them. And that's where I get into trouble.
> 
> Here's the code I'm using:
> 
>         def substitute(name, value, cts):
>             """
>             Finds all the occs in  cts of $<name>
>             and replaces them with value
>             """
>             
>             pat = re.compile("\$<" + name + ">", re.IGNORECASE)
> 
>             return pat.sub(val, cts)  # this line causes the error (see below)
> 
>         def escapeMetachars( s ):
>             """
>             All metacharacters in the user provided substitution must
>             be escaped
>             """
>             meta = r'\.^$+*?{[|()'
>             esc = ''
> 
>             for c in s:
>                 if c in meta:
>                     esc += '\\' + c
>                 else:
>                     esc += c
> 
>             return esc
> 
> cts = """Compression=bzip/9
> OutputBaseFilename=$<OutputFileName>
> OutputDir=$<OutputDir>
> LicenseFile=Z:\apps\easyjob\main\I18N\US\res\license.txt"""
> 
> name = 'OutputDir'
> value = "c:\\apps\\whatever\\"  # contains the backslash metachar
> 
> print substitute( escapeMetachars(name), value,  cts)
> 
> I get this error:
> Traceback (most recent call last):
>   File "<pyshell#38>", line 1, in -toplevel-
>     pat.sub(s,cts)
>   File "C:\ARCHIV~1\python23\Lib\sre.py", line 257, in _subx
>     template = _compile_repl(template, pattern)
>   File "C:\ARCHIV~1\python23\Lib\sre.py", line 244, in _compile_repl
>     raise error, v # invalid expression
> error: bogus escape (end of line)
> 
> What on earth is this? O:-)
> 
> PS: I can't use string.replace() for the susbtitution,because it must be
> case-insensitive: the user might enter OUTPUTDIR, and it should still work.

The following works:

import re

cts = """Compression=bzip/9
OutputBaseFilename=$<OutputFileName>
OutputDir=$<OutputDir>
LicenseFile=Z:\\apps\\easyjob\\main\\I18N\\US\\res\\license.txt"""

name = 'OutputDir'

pat = re.compile("\$<" + name + ">", re.IGNORECASE)

value = "c:\\apps\\whatever\\"

def escape(s):
     return s.replace('\\', '\\\\')

print pat.sub(escape(value), cts)

Note, that you should double \ in cts too (at least my snake prints some 
garbage otherwise).

regards,
anton.





More information about the Python-list mailing list