Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, July 16, 2009 3:12 PM
Greetings,
I would like to create a regex expression to remove all characters from a string except the thousand separator and decimal separator. For example:
"12345" returns 12345
"1,234" returns 1234
"1,234.5" returns 1234.5
"1234/5" returns 12345
Note that the thousand and decimal separator are use default culture. Please help anyone?
Regards
Bughata
All replies (8)
Thursday, July 16, 2009 9:38 PM âś…Answered
Your original question said you want to keep the thousands separator, but your example shows you want to remove them. Please tell us which is correct? You can use the CultureInfo.NumberFormat to determine what the thousand separator is and what the decimal separator is. Here's an example...
ci = new CultureInfo("de-DE");
Console.WriteLine("Decimal ({0})", ci.NumberFormat.CurrencyDecimalSeparator);
Console.WriteLine("Thousands ({0})", ci.NumberFormat.CurrencyGroupSeparator);
You can then build your pattern like this...
string pattern = String.Format(@"[^\d\0}\1}]",ci.NumberFormat.CurrencyDecimalSeparator, ci.NumberFormat.CurrencyGroupSeparator);
and finally,
string stripped = Regex.Replace(numberString, pattern, "");
This preserves digits, decimals and separators.Les Potter, Xalnix Corporation, Yet Another C# Blog
Thursday, July 16, 2009 3:27 PM
Hi,
Replace all things that are not a number, which is this...
[^\d]
Regex.Replace("1,234.5", "[^\d]", "")
www.dsmyth.net | www.dsmyth.net/wiki
Thursday, July 16, 2009 3:57 PM
Thanks for the quick reply. Your expression also removes decimal point.
>>Regex.Replace("1,234.5", "[^\d]", "") line returns 12345 which is not correct. I want decimals included. I have tried [^\d\] but not sure its correct.
Regards
Bughata
Thursday, July 16, 2009 4:04 PM
Use "[^1234567890.]" or "[^0-9.]"
If you want to use the culture, you'll have to build the compare string to insert whatever the decimal separator is.
Ron Whittle - If the post is helpful or answers your question, please mark it as such.
Thursday, July 16, 2009 4:17 PM
You can use this pattern: [^\d\,]
StringBuilder sb = new StringBuilder();
sb.AppendLine( "12345" + " " + Regex.Replace( "12345", @"[^\d\.,]", "" ) );
sb.AppendLine( "1,234" + " " + Regex.Replace( "1,234", @"[^\d\.,]", "" ) );
sb.AppendLine( "1,234.5" + " " + Regex.Replace( "1,234.5", @"[^\d\.,]", "" ) );
sb.AppendLine( "1234/5" + " " + Regex.Replace( @"1234/5", @"[^\d\.,]", "" ) );
MessageBox.Show( sb.ToString() );
HTHhttp://iborn2code.blogspot.com/
Thursday, July 16, 2009 4:50 PM
You can use the Regex.Replace as described above with a MatchEvaluator. The match evaluator would decide if the non numeric character should be removed or left behind based upon the Current Culture. The example uses the MatchEvaluator. The evaluator specifically creates a new culture so that I can determine that cultures decimal point (using a kludge). I'm sure there is a better way to determine a cultures decimal point. The main point of the example is that the MatchEvaluator can be used on the fly to choose what character to remove or allow. Here's the example...
string test = "1,234.5";
string pattern = @"[^\d]";
Console.WriteLine("{0}",Regex.Replace(test,pattern,delegate (Match m)
{
CultureInfo ci;
ci = new CultureInfo("de-DE");
float f = 0.1F;
string decpoint = f.ToString(ci).Substring(1,1);
Console.WriteLine("Decimal Point is ({0})", decpoint);
if(m.Value == decpoint)
return m.Value;
return "";
}));
Les Potter, Xalnix Corporation, Yet Another C# Blog
Thursday, July 16, 2009 5:19 PM
I understand you might have your heart set on Regular Expressions for your own reasons, but it never hurts to throw an extra iron in the fire just incase. If you're willing to give Lambda Expressions a chance (and you're on Fx 3.0+), you could use the following:
private static string parseNumerics(string value)
{
return parseNumerics(value, '.');
}
private static string parseNumerics(string value, params char[] otherAllowableCharacters)
{
return new string(value.ToCharArray().Where((holdChar) =>
Char.IsDigit(holdChar) || otherAllowableCharacters.Contains(holdChar)).ToArray());
}
Thursday, July 16, 2009 10:23 PM
Les! perfect solution,
I hope user doesn't set CurrencyGroupSeparator or CurrencyDecimalSeparator to something weird.
ci.NumberFormat.CurrencyGroupSeparator = @"d";
Good job.
http://iborn2code.blogspot.com/