senzacionale
senzacionale

Reputation: 20906

regex number problem

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

Upvotes: 0

Views: 324

Answers (4)

Amber
Amber

Reputation: 526573

\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+)?

Upvotes: 3

mpen
mpen

Reputation: 282825

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

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

Upvotes: 1

Jürgen Steinblock
Jürgen Steinblock

Reputation: 31723

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);

Upvotes: 1

Philippe Leybaert
Philippe Leybaert

Reputation: 171744

\d+((\.\d+)|(,\d+))?

Upvotes: 1

Related Questions