2

I'm reading text files that has decimal numbers in them. They can be made in "any" Culture so the decimal point may be different to the current culture.

So I use a regex to replace all different CurrencyDecimalSeparators with the current.

I'm using this code

string output = Regex.Replace(inValue, @"[\.\?,;/-]", CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator);

e.g. 1.25 -> 1,25 (I'm in Sweden)

Now to my question: When I first tried this I was using this regexp @"[\.,;/-\?]" which didn't work. All characters where replaced with the current separator.

e.g. 1.25 -> ,,,,

Why?

// Anders

1 Answer 1

6

It's because of /-\?. This is a character range of / to ? (the ? is being escaped).

This range covers:

/ 0 1 2 3 4 5 6 7 8 9 : ; < = > ?

If you see a unicode table

enter image description here

2
  • 1
    So it's the - that makes it a range. So it can be solved by escaping the - like \-
    – Andis59
    Feb 17, 2014 at 12:42
  • 1
    @Andis59: Or by using - as the first or last character in the group.
    – Heinzi
    Feb 17, 2014 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.