Making the substitution string raw in re.sub()
Fredrik Lundh
fredrik at effbot.org
Tue Oct 31 18:13:30 EST 2000
Joakim Hove wrote:
> I'm using the re module to perform some substitutions on a string:
>
> OldString = "See for instance EXPAND(key1)"
> newString = r"Volume {\bf 45}"
> print re.sub(r"EXPAND\(\s*(\w+?)\s*\)" , newString , OldString , 1)
>
> But, when I run this the newString is not properly escaped, and
> what I get is:
> >>> See for instance Volume f 45}
>
> instead of:
>
> >>> See for instance Volume {\bf 45}
>
> Which is what I wanted. ANy tips to how I Could solve this??
re.escape is your friend:
newString = re.escape(r"Volume {\bf 45}")
</F>
<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->
More information about the Python-list
mailing list