trouble with regex with escaped metachars (URGENT please O:-)
Roel Mathys
rm at rm.net
Thu Nov 20 12:48:01 EST 2003
Fernando Rodriguez wrote:
> On Thu, 20 Nov 2003 16:13:19 GMT, Roel Mathys <rm at rm.net> wrote:
>
>
>
>>>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:-)
>>
>>it's the value of "value" that gives trouble (ending with a "bogus" \
>>followed by an (invisible) end-of-line.
>>This little patch will do the trick, and apparantly
>
>
> But the final backslash is escaped, it shouldn't be a problem... :-(
>
>
like I said, don't know
even stranger, with this value it does work
=> value = "c:\\apps\\whatever\\\\"
but like I showed in the previous post, you don't really need a regex
for this.
eg:
def substitute2( name , value , cts ) :
ucts = cts.upper()
uname = name.upper()
parts = ucts.split( r'$<' + uname + '>' )
if len( parts ) != 2 :
raise 'Something'
return value.join( [ cts[:len(parts[0])] , cts[-len(parts[1]):]])
will do the job just as fine
bye,
rm
More information about the Python-list
mailing list