Simple regex that has me banging my head against a wall

William Park parkw at better.net
Wed Mar 21 17:48:44 EST 2001


On Wed, Mar 21, 2001 at 02:31:44PM -0800, Timothy Grant wrote:
> Hi again.
> 
> I actually thought I had this regex thing down pretty well, but
> I'm getting beat up by what I thought should be a simple one.
> 
> >>> x = '$1$asdfjke$lkjasdkjfsdasdfj'
> >>> re.sub(r'\$', r'\\\$', x)
> '\\\\$1\\\\$asdfjke\\\\$lkjasdkjfsdasdfj'
> 
> All I want to do is substitute \$ for $.

Solution:
    >>> re.sub('\$', r'\$', x)
    '\\$1\\$asdfjke\\$lkjasdkjfsdasdfj'

Note: 2nd argument is just string, not pattern.

:wq --William Park, Open Geometry Consulting, Linux/Python, 8 CPUs.




More information about the Python-list mailing list