2

I want to create a regular expression for all sorts of numbers i.e whole numbers (+ve and -ve) and decimal numbers (+ve and -ve) with or without commas.

For e.g the regular expression should cover following number formats.

  111     1.11     1,111     1,111.01     1,111,111.01 
 +111    +1.11    +1,111    +1,111.01    +1,111,111.01
 -111    -1.11    -1,111    -1,111.01    -1,111,111.01

I have created two regular expressions for handling my scenario.

 "^(\\+|-)?[0-9]\\d*(\\.\\d+)?$"   // handles whole numbers with decimals
 "^(\\+|-)?[0-9]\\d*(\\,\\d+)*?$"  // handles whole numbers with commas

Now, I want to merge these two regular expressions in order to address my requirements.

Can anyone help me?
Thanks in advance.

Suniel
  • 1,449
  • 8
  • 27
  • 39
  • Possible duplicate of [Decimal number regular expression, where digit after decimal is optional](http://stackoverflow.com/questions/12117024/decimal-number-regular-expression-where-digit-after-decimal-is-optional) [Specifically the answer here](http://stackoverflow.com/a/23872060/2064981) – SamWhan Dec 13 '16 at 09:52
  • 1
    @ClasG: [That one does not help](https://regex101.com/r/68lRyr/1). – Wiktor Stribiżew Dec 13 '16 at 09:58

3 Answers3

2

What about this one:

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

You can see it working here.

enrico.bacis
  • 30,497
  • 10
  • 86
  • 115
  • Not working for more than one comma. But thanks for the idea. – Suniel Dec 13 '16 at 10:30
  • 1
    @Suniel, you understand you didn't ask for that in the requirements or in the tests, right? Anyway just change `(,\d+)?` with `(,\d+)*` and it will work as expected. – enrico.bacis Dec 13 '16 at 10:31
  • Actually i added * but in a wrong place. My mistake. Many thanks to you. – Suniel Dec 13 '16 at 10:37
2

Here is my solution that allows only 3 digits between comma:

^[+-]?\d{1,3}(?:,\d{3}|\d+)*(?:\.\d+)?$

Explanation:

^           : start of string
[+-]?       : optional + or -
\d{1,3}     : 1 to 3 digits (before the first optional comma)
(?:         : non capturing group
  ,\d{3}    : a comma followed by 3 digit
  |         : OR
  \d+       : 1 or more digits
)*          : group present 0 or more times
(?:         : non capturing group
  \.\d+     : decimal dot followed by 1 or more digits
)?          : optional
$           : end of string
Toto
  • 89,455
  • 62
  • 89
  • 125
1

You may merge these 2 patterns of yours like

^[+-]?[0-9]+(?:,[0-9]+)*(?:[.][0-9]+)?$

See the regex demo

Details:

  • ^ - start of a string
  • [+-]? - an optional + or -
  • [0-9]+ - 1 or more digits
  • (?:,[0-9]+)* - zero or more sequences of:
    • , - a comma
    • [0-9]+ - 1 or more digits
  • (?:[.][0-9]+)? - an optional sequence of:
    • [.] - a dot
    • [0-9]+ - 1+ digits
  • $ - end of string

A more restrictive regex to only allow 3-digits in groupings will look like

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

And if you want to also match whole parts with no thousand separators:

^[+-]?(?:[0-9]{1,3}(?:,[0-9]{3})*|[0-9]+)(?:[.][0-9]+)?$
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563