Search code examples
pythonregexreplacedollar-sign

String replace all dollar amounts with a specfic string


I need to replace dollar amounts with a"" tag in string in python. This is what I have figured out so far:

Here is my string:

s = 'Accounts and current portion of notes receivable, net of allowances of $2,199 and $2,506 at July 2, 2011 and October 2, 2010, respectively'

With this regular expression I can find all the dollar amounts correctly.

re.findall(r"[\$]{1}[\d,]+\.?\d{0,2}",s)

gives me:

['$2,199', '$2,506']

However, I want to replace the dollar amounts with "" in the original string. How do I do that?

Expected output:

'Accounts and current portion of notes receivable, net of allowances of <amount> and <amount> at July 2, 2011 and October 2, 2010, respectively'

Solution

  • maybe

    re.sub(r"[\$]{1}[\d,]+\.?\d{0,2}","<amount>",s)
    

    will do what you need... btw if you need just one you dont have to specify {1} as that is the default behaviour