locked
replace "," and "$" to blank RRS feed

  • Question

  • User924526831 posted

    I need to replace all the occurances of "," and "$" to blank in a numeric value. What should I write in a Regex pattren

    Example - Regex.replace("$199,79,80",'Pattern", "")

    output should be 1997980

    Your help is much appreciated. Thanks.

     

     

    Monday, March 28, 2011 3:12 PM

Answers

  • User-1501102275 posted

    Here you go:

    string input = "$199,79.80";
    string output = Regex.Replace(input, @"(\$|,)+", String.Empty);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 28, 2011 3:24 PM

All replies

  • User-1501102275 posted

    Here you go:

    string input = "$199,79.80";
    string output = Regex.Replace(input, @"(\$|,)+", String.Empty);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 28, 2011 3:24 PM
  • User1324895001 posted

    you can try this:

     

    number = Regex.Replace(number, @"\D", "");
    
    \D means all non-numeric chars are replaced by "".
    
    
    Monday, March 28, 2011 3:26 PM
  • User-507673006 posted
    I can only think of a method without Regex at the moment (sorry if it's required):
    string inputString = "$199,79,80";
    string outputString = "";
    foreach (char c in inputString)
    {
          if (c.ToString == "$" || c.ToString == ",")
          {
          }
          else
          {
                outputString += c;
          }
    }
    Hope it helps!
    Monday, March 28, 2011 3:29 PM
  • User-1501102275 posted

    number = Regex.Replace(number, @"\D", "");

    If you use "\D", I believe you will lose your decimal.

    Monday, March 28, 2011 3:29 PM
  • User1324895001 posted

    The original post doesnt have decimal, it has 2 commas..

    Monday, March 28, 2011 3:36 PM
  • User-1501102275 posted

    True. I was assuming since "$199,79,80" is not a valid money amount, that the last comma was intended to be a decimal and the other was just in the wrong spot.

    Monday, March 28, 2011 3:42 PM