2010-11-08 32 views
4

我试图从字符串值中删除任何货币符号。正则表达式从字符串中移除任何货币符号?

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

这可行,但它不会删除阿拉伯语中的货币符号。我不知道为什么。

以下是带货币符号的示例阿拉伯字符串。

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

您是否在表达式中尝试过使用'CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol'? – 2010-11-08 01:21:16

+0

@mootinator,你看到单词'any'吗?您的解决方案将只取代'当前'货币符号 – 2011-12-09 10:01:00

+5

@ taras.roshko您在评论发布13个月后是否养成了讽刺言论的习惯?显然有一个原因是评论而不是答案。 – 2011-12-09 15:13:49

回答

9

不符合符号 - 使表达式匹配数字。

尝试这样:

([\d,.]+) 

有太多的考虑到货币符号。最好只捕获你想要的数据。前面的表达式将捕获数字数据和任何地方分隔符。

使用这样的表达:

var regex = new Regex(@"([\d,.]+)"); 

var match = regex.Match(txtPrice.Text); 

if (match.Success) 
{ 
    txtPrice.Text = match.Groups[1].Value; 
} 
+0

这是错误的输出是txtPrice.Text =“جم”; – 2010-11-08 01:30:21

+0

请参阅我的示例代码 - 您不希望使用Regex.Replace这个表达式。 – 2010-11-08 01:40:57

+0

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) { string pattern = @“([\ d,。] +)”; Regex rgx = new Regex(pattern); string x = rgx.Replace(txtPrice.Text,“”); txtPrice.Text = x; } – 2010-11-08 01:45:05

0

从安德鲁野兔的答案几乎是正确的,你可以随时使用\ d匹配数字*,它将任何数字匹配问题文本。

相关问题