Regular expression to remove any currency symbol from a string?

前端 未结 2 867
既然无缘
既然无缘 2021-01-13 06:25

I\'m trying to remove any currency symbol from a string value.

using System;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Win         


        
相关标签:
2条回答
  • 2021-01-13 06:37

    Don't match the symbol - make an expression that matches the number.

    Try something like this:

     ([\d,.]+)
    

    There are just too many currency symbols to account for. It's better to only capture the data you want. The preceding expression will capture just the numeric data and any place separators.

    Use the expression like this:

    var regex = new Regex(@"([\d,.]+)");
    
    var match = regex.Match(txtPrice.Text);
    
    if (match.Success)
    {
        txtPrice.Text = match.Groups[1].Value;
    }
    
    0 讨论(0)
  • 2021-01-13 06:37

    The answer from Andrew Hare is almost correct, you can always match digit using \d.*, it will match any digit to your text in question.

    0 讨论(0)
提交回复
热议问题