Regular Expressions 101

Community Library

Community Library Entry

0

Regular ExpressionOpen Workspace

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

Description
Created·2022-10-27 17:25
Updated·2023-07-20 05:54
Type·Match
Flavor·Java

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
Open Workspace