Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx matcing numeric values with or without thousand separators

Tags:

regex

I need a RegEx to match the following:

1.234.567
-1.234.789
1234567
-1234567

It should not match:

.123     (leading separator)
123..456 (two separators)

In other words, I need a RegEx to match long numeric values formatted with or without thousand separators.

Thanks!

like image 877
Olav Haugen Avatar asked Jun 06 '11 06:06

Olav Haugen


2 Answers

Here is a more restricted answer

^-?(?!0)(?:\d+|\d{1,3}(?:\.\d{3})+)$

See it online here at Regexr

(?!0) prevents from starting with 0

\d+ allows the numbers without separator

\d{1,3}(?:.\d{3})+ is the part for the separator. Start with 1 to 3 numbers, then a separator and 3 numbers. The dot for the separator followed by 3 numbers can be repeated.

like image 107
stema Avatar answered Nov 07 '22 22:11

stema


You could do something like

^-?([0-9]{1,3}\.?)+$

RegExr Demo

like image 2
kapa Avatar answered Nov 07 '22 20:11

kapa