开发者

Regular expression to remove any currency symbol from a string?

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

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        string pattern = @"(\p{Sc})?";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            decimal x = 60.00M;
            txtPrice.Text = x.ToString("c");
        }

        private void btnPrice_Click(object sender, EventArgs e)
        {
            Regex rgx = new Regex(pattern);开发者_C百科
            string x = rgx.Replace(txtPrice.Text, "");
            txtPrice.Text = x;
        }
    }
}
// The example displays the following output:
// txtPrice.Text = "60.00";

This works, but it won't remove currency symbols in Arabic. I don't know why.

The following is a sample Arabic string with a currency symbol.

txtPrice.Text = "ج.م.‏ 60.00";


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;
}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜