Regular Expressions 101

Community Patterns

Regular expression for positive number with 2 decimals used for displaying money

0

Regular Expression
Java 8

"
^[1-9]\d*(?[\,\.]\d{3})?$
"
gm

Description

I think from time to time we all need a regular expression for displaying numbers with 2 decimals for displaying money of various currencies in the world. This regex should only allow 1 dot or comma as a decimal separator. And only allow positive numbers and the numbers can't begin with a 0.

Goals:

  • Both dot and comma can be used as a decimal separator
  • The decimal separator can only be once in a number
  • Numbers can't begin with a zero
  • Only positive numbers allowed
Submitted by Remzi Cavdar - a year ago (Last modified 8 months ago)