Regular expression to remove any currency symbol from a string?

I am 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 will not remove currency symbols in Arabic. I do not know why.

The following is an example of an Arabic string with a currency symbol.

 txtPrice.Text = "Ψ¬.Ω….‏ 60.00"; 
+4
source share
2 answers

Do not match the character - enter the expression corresponding to the number.

Try something like this:

  ([\d,.]+) 

Too many currency symbols to account for. It’s best to just capture the data you need. In the previous expression, only numeric data and any place delimiters will be displayed.

Use an expression like this:

 var regex = new Regex(@"([\d,.]+)"); var match = regex.Match(txtPrice.Text); if (match.Success) { txtPrice.Text = match.Groups[1].Value; } 
+9
source

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

0
source

All Articles