Removing invalid characters from price

前端 未结 7 422
臣服心动
臣服心动 2021-01-15 15:40

I have a scenario where I have to remove certain characters from a price string using C#.

I\'m looking for a regular expression to remove these characters or somethi

相关标签:
7条回答
  • 2021-01-15 16:09

    You can use following regular expression

    Case 1: if ( Ex.TAX ) is constant, you can just remove the text using string function String.Replace.

    Case 2: if you require number which contains only , following is the regex you can use to extract the same

    [0-9,]{1,}

    Case 3: if ( is there always there after number, the following regex can be used

    \d.*(?=\()

    following is c# code for regex

    public static Regex regex = new Regex(
          "\\d.*(?=\\() ",
        RegexOptions.IgnoreCase
        | RegexOptions.CultureInvariant
        | RegexOptions.IgnorePatternWhitespace
        | RegexOptions.Compiled
        );
    //// Capture the first Match, if any, in the InputText
    // Match m = regex.Match(InputText);
    
    0 讨论(0)
  • 2021-01-15 16:09

    To find "( Ex. TAX )" try this regex:

    /\( Ex\. TAX \)/i
    
    0 讨论(0)
  • 2021-01-15 16:10
     Regex rex = new Regex(@"(?:(?:\d{1,2},)?(?:\d{3},)*(?:\d{3})(?:\.\d+){0,})|(\d+)");
    
     Console.WriteLine(rex.Match("3,950,000 ( Ex. TAX )").Groups[0].Captures[0].Value);
     Console.WriteLine(rex.Match("3,950,000,000").Groups[0].Captures[0].Value);
     Console.WriteLine(rex.Match("3,950,000,000UHFWF#FWHFWEFE").Groups[0].Captures[0].Value);
     Console.WriteLine(rex.Match("3,950,000,000,000,000.00").Groups[0].Captures[0].Value);
    

    Output:

    • 3,950,000
    • 3,950,000,000
    • 3,950,000,000
    • 3,950,000,000,000,000.00
    0 讨论(0)
  • 2021-01-15 16:11
    String price = "3,950,000 ( Ex. TAX)".Replace(" ( Ex. TAX)","");
    
    0 讨论(0)
  • 2021-01-15 16:21

    Why use a RegEx when a simple replace will do?

    string clean = "3,950,000 ( Ex. TAX )".Replace(" ( Ex. TAX )", string.Empty);
    
    0 讨论(0)
  • 2021-01-15 16:24

    try this

    myPrice.Replace(" ( Ex. TAX ),"")
    
    0 讨论(0)
提交回复
热议问题