1

Seems pretty simple but i'm having problems coming up with a good method to do this. This should be possible with the regex.replace in .NET VB... Sorry but I'm fairly rookie at Regex...

I have a long string with contains a sales receipt.... I do not want to display the dollar amounts under certain instances, so i want to replace all amounts with 0.00 before displaying the string. ie.

original string:

Order Details: item one 1 @ $33.99 $33.99 item two 100 @ $3.99 $399.00 item three 10 @ $333.99 $3339.90 Subtotal: 33333.99 Shipping: 10.27 Tax: 0.00 Total: 444444.26

after replace:

Order Details: item one 1 @ $0.00 $0.00 item two 100 @ $0.00 $0.00 item three 10 @ $0.00 $0.00 Subtotal: 0.00 Shipping: 0.00 Tax: 0.00 Total: 0.00

Thanks, Todd.

4 Answers4

2

Regex

\d+\.\d{2}

Regular expression visualization

Debuggex Demo

Description

This will find all your numbers then simply replace with 0.00

If you wanted something that found the $ symbol as well the below regex would be right:

^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*\.[0-9]{2}$

This finds groups of 3 with , as well however that isn't what your example showed.

abc123
  • 16,261
  • 6
  • 46
  • 76
1

See this:

What is "The Best" U.S. Currency RegEx?

Replace on this expression: [+-]?[0-9]{1,3}(?:,?[0-9]{3})*\.[0-9]{2} and you'll get what you're looking for.

Community
  • 1
  • 1
dotnetnate
  • 771
  • 4
  • 8
1

This should do the trick.

Regex.Replace(input, "[0-9]+\.[0-9]{2}", "0.00");

See working demo

hwnd
  • 65,661
  • 4
  • 77
  • 114
  • fyi, the part that makes sure `,`s appear with exactly 3 digits afterwards doesn't work- it actually checks for at least 3. – OGHaza Dec 15 '13 at 21:03
0

use this pattern (\$)[\d.]+|(otal:)\s*[\d.]+ with g Option
and replace with $1$2 0.00 Demo

alpha bravo
  • 7,292
  • 1
  • 14
  • 22