tags:

views:

45

answers:

4

this is my regex for digital dnumbers:

\d+(.\d+)+(,\d+)

but now i have problem that number 3 or 30 are not valid any more. What must be my regex that also number 3 and 40 will pass.

Thx

+3  A: 
\d+(\.\d+)*(,\d+)?

The + in regex means "at least one", whereas the * means "zero or more" and the ? means "either one or none".

Also, you have to escape periods as \. since otherwise the . character is a special character in regex meaning "any single character".

If you want to make sure that the .'s in the number (if present) always separate digits by groups of 3, you could use this (the {x} syntax means "exactly x repetitions"):

\d+(\.\d{3})*(,\d+)?

Or to force thousands separators all the time, you could use this (the {x,y} syntax means "anywhere from x to y repetitions):

\d{1,3}(\.\d{3})*(,\d+)?
Amber
Your last one will match `12000.423`; it doesn't enforce the first chunk to have thousands separators.
Mark
@Mark: Yes, that was intentional (in order to match numbers like `12000,34`). But if forcing thousands separators is desired, then simply changing the leading `\d+` to be `\d{1,3}` instead will work.
Amber
Ok :) Just checkin. I gave you a +1 anyway.
Mark
+1  A: 
\d+((\.\d+)|(,\d+))?
Philippe Leybaert
+1  A: 

If what you really want is . for thousands separator, and , for the decimal separator, try this:

\d{1,3}(\.\d{3})*(,\d+)?
Mark
I think the OP is more just trying to parse numbers in the standard European format, where the `,` is the decimal separator, and the `.` is the digit grouping mark.
Amber
@Amber: Oh...that wasn't clear. Edited.
Mark
+1  A: 

so you want a regex that matches 1 and 3.000 and 3.000,5 ?

If you don't want to capture the result this should do:

[.\d]+(,\d+)?

but keep in mind that this is not very accurat anyway since it also matches 2.0.0,12 and you should also include a plus minus check:

^(\+|-)?[.\d]+(,\d+)?

In C# you could do better with

double result;
bool isDouble = Double.TryParse("3.000,5", Globalisation.CultureInfo.InvariantCulture);
SchlaWiener