我正在尝试从字符串值中删除任何货币符号.
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";
Run Code Online (Sandbox Code Playgroud)
这有效,但不会删除阿拉伯语中的货币符号.我不知道为什么.
以下是带有货币符号的示例阿拉伯字符串.
txtPrice.Text = "?.?.? 60.00";
Run Code Online (Sandbox Code Playgroud)
与符号不匹配 - 创建与数字匹配的表达式.
尝试这样的事情:
([\d,.]+)
Run Code Online (Sandbox Code Playgroud)
有太多的货币符号需要考虑.最好只捕获您想要的数据.前面的表达式将仅捕获数字数据和任何位置分隔符.
使用这样的表达式:
var regex = new Regex(@"([\d,.]+)");
var match = regex.Match(txtPrice.Text);
if (match.Success)
{
txtPrice.Text = match.Groups[1].Value;
}
Run Code Online (Sandbox Code Playgroud)