Removing invalid characters from price

前端 未结 7 421
臣服心动
臣服心动 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:25

    Regular expressions are always tricky to get right, since the input can vary so greatly, but I think this one covers your needs:

    string pattern = @"([\d]+[,.]{0,1})+";
    string cleanedPrice = Regex.Match(price, pattern).Value;
    

    Explained:

    (         - start matching group
    [\d]+     - match any decimal digit, at least once
    [,.]{0,1} - ...followed by 0 or 1 comma or dot
    )         - end of group
    +         - repeat at least once
    

提交回复
热议问题