Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.

Can anyone tell me how to make a regex for this problem. I have an issue with currency where sometimes people by mistake types a '.' instead of a ',' However it should be fine to use the '.' to seperate numbers

1,0
1.0 <- fail
1,92
1.92 <- fail
100,2
100.2 <- fail
100,02
100.02 <-- fail
1.000,00
1.000.00 <- fail
1.000.000,01
1.000.000.02 <- fail

The plan is to use it as a testing on an aspx box. I know how to add a regex but i fail utterly in trying to make a working regex for this. If anyone can help out with an explanation i would truly appreciate it
Posted
Comments
Kenneth Haugland 15-Oct-13 3:47am    
How about ToString.RePlace(",",".") or something like that?
Member 10121607 15-Oct-13 3:50am    
Hello. I tried to set up some replace. But it will not work for me. Example this: 1.000.00 would then turn into 1,000,00 and that will not do :( This is the reason im looking for some regex expression instead.

Using the build in functions in .NET might be the way to go:
C#
using System;
using System.Globalization;
class Sample
{
public static void Main()
{
CultureInfo ciClone = null;
double value = 0;
try {
ciClone = (CultureInfo)CultureInfo.InvariantCulture.Clone();
ciClone.NumberFormat.NumberDecimalSeparator = ",";
ciClone.NumberFormat.NumberGroupSeparator = ".";
value = Double.Parse("1.000,05", ciClone);
Console.WriteLine("The value is {0}", value);
}

catch (Exception e) {
Console.WriteLine(e);
}
}
}



If you want to use regex you could try this:
Expresso - A Tool for Building and Testing Regular Expressions[^]

and build one for yourself.
 
Share this answer
 
Try this:
\.\d+$
If it matches, it's wrong according to your definitions.

As Kenneth said : get a copy of Expresso! Lovely piece of software...
 
Share this answer
 
This should work for you:
"^\d{1,3}(\.\d{3})*,\d{1,2}$"

It matches a number ending with comma followed by one or two digits: ",\d{1,2}", having optional digit triplets separated by a 'dot' thousands separator: "(\.\d{3})*" and starting with one to three digits: "^\d{1,3}".
If you want an optional fraction part, use "(,\d{1,2}){0,1}$" instead of ",\d{1,2}".
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900