using re module to find " but not " alone ... is this a BUG in re?
Duncan Booth
duncan.booth at invalid.invalid
Thu Jun 12 08:35:57 EDT 2008
John Machin <sjmachin at lexicon.net> wrote:
> What you want is:
>
>>> import re
>>> text = r'frob this " avoid this \", OK?'
>>>> text
> 'frob this " avoid this \\", OK?'
>>> re.sub(r'(?<!\\)"', r'\"', text)
> frob this \\" avoid this \\", OK?'
>>>
>
Or you can do it without using regular expressions at all. Just replace
them all and then fix up the result:
>>> text = r'frob this " avoid this \", OK?'
>>> text.replace('"', r'\"').replace(r'\\"', r'\"')
'frob this \\" avoid this \\", OK?'
--
Duncan Booth http://kupuguy.blogspot.com
More information about the Python-list
mailing list