Comma c# in regular expresson RRS feed

  • Question

  • Hello,

    I have a regular expression in some C# code that's being used to strip out formatting from a number:


    Any idea what the


    part of this regular expression is supposed to do?  Here's an example of the regular expression in use:

    Convert.ToDouble(Regex.Replace("-1000000.00", "[^0-9,-,.]", ""))

    I tried leaving it out and couldn't see any difference in the results with the numbers I was looking at.  I've searched through several online regular expression tutorials and can't find any that use commas like this. 

    I find some that use commas that indicate something needs to be repeated, e.g.:

    {n,m} Repeat at least n, but no more than m times

    but this is different.


    Tuesday, May 7, 2013 1:56 PM


  • The comma doesn't have any special meaning in the regex character groups, so the pattern above reads:

    Match any character that is not one of the following:

    - a character between '0' and '9'

    - a character between ',' and ',' (that's weird, I know)

    - a period.

    As a result, the minus sign is not listed as an allowed character and your code removes the minus sign from "-10000.00" (tested on .NET 4.5, I'm not sure what happens on other platforms).

    I doubt that's the intended behavior; I would suggest that you rewrite your pattern based on what you expect to be preserved.

    O.T.: just removing offending characters does not guarantee that you end up with a string that can be converted successfully to a double (e.g. "") and, conversely, can make a totally bogus string into a valid number, which is equally bad (e.g. "5/16"). Depending on your needs, there could be better solutions.


    • Proposed as answer by Mike Feng Wednesday, May 8, 2013 1:24 PM
    • Edited by Mario Cossi Thursday, May 9, 2013 5:05 AM
    • Marked as answer by Mike Feng Sunday, May 12, 2013 8:20 AM
    Tuesday, May 7, 2013 4:00 PM