Simple regex that has me banging my head against a wall

Drew Csillag drew.csillag at starmedia.net
Wed Mar 21 18:27:16 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 $.

How about:
Python 2.1b1 (#1, Mar  4 2001, 11:31:25) 
[GCC 2.95.2 19991024 (release)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> x = '$1$asdfjke$lkjasdkjfsdasdfj'
>>> import string
>>> print string.replace(x, '$', r'\$')
\$1\$asdfjke\$lkjasdkjfsdasdfj


-- 
Drew Csillag                                              
Acting Manager of Platform Engineering
drew.csillag at starmedia.net        Cell: (917)-679-8827  
StarMedia Network, Inc.           Land: (212)-905-8332 




More information about the Python-list mailing list