正则表达式用来删除字符串的任何货币符号? [英] Regular expression to remove any currency symbol from a string?

查看:734
本文介绍了正则表达式用来删除字符串的任何货币符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图删除一个字符串值的货币符号

 使用系统;使用System.Windows.Forms的
;使用System.Text.RegularExpressions
;

命名空间WindowsFormsApplication1
{
公共部分Form1类:表格
{
字符串模式= @(\p {}钪)?
公共Form1中()
{
的InitializeComponent();
}

私人无效Form1_Load的(对象发件人,EventArgs五)
{
十进制X = 60.00M;
txtPrice.Text = x.ToString(C);
}

私人无效btnPrice_Click(对象发件人,EventArgs五)
{
正则表达式RGX =新的正则表达式(模式);
字符串x = rgx.Replace(txtPrice.Text,);
txtPrice.Text = X;
}
}
}
//的例子显示的输出如下:
// txtPrice.Text =60.00;

这工作,但它不会在阿拉伯语中移除货币符号。我不知道为什么。



下面是一个简单的阿拉伯语字符串以货币符号。

  txtPrice.Text =ج.م.60.00; 


解决方案

不要符号匹配 - 使一个表达式。匹配的数量



尝试是这样的:

 ([ \d。] +)

有太多的占货币符号。这是更好地只捕获所需的数据。 。前面的表达式将捕获只是数字数据和任何地方分隔



使用这样的表达:

  VAR的regex ​​=新的正则表达式(@([\d。] +)); 

VAR匹配= regex.Match(txtPrice.Text);

如果(match.Success)
{
txtPrice.Text = match.Groups [1] .value的;
}


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

这篇关于正则表达式用来删除字符串的任何货币符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆