Home > Mobile >  Regular expression for CSV with decimal numbers or integers
Regular expression for CSV with decimal numbers or integers

Time:08-31

  • 1,2,3,4 - Should pass
  • 1, - Should fail
  • 1.2,1,4,5 - Should pass
  • 1.,2 - Should fail
  • 1.2,1.2 - Should pass
  • .5,.5 - Should fail

CodePudding user response:

As the regex flavor is not specified, let me first assume PCRE. Then would you please try:

^\d (?:\.\d )?(?:,\d (?:\.\d )?)*$
  • \d (?:\.\d )? matches one or more digits optionally followed by a dot and one or more digits.
  • (?:,\d (?:\.\d )?)* matches zero or more units which consists of a comma and the number defined as above.
  • ^ and $ anchor the start/end of the string individually.

Demo

In case of ERE you can say instead:

^[0-9] (\.[0-9] )?(,[0-9] (\.[0-9] )?)*$
  • Related