AllDayer
AllDayer

Reputation: 993

Regex for optional comma separated number with decimal point

I'm after a regular expression which will match the following

1,245.30
24,235,235.50
12235235.60
235.50

The decimal is mandatory but I'd like the comma to be optional. Based off this answer Regex for Comma Separated Number I was able to come up with this expression which works great for the comma separated numbers.

(^\d{1,3}([,]\d{3})*([.]\d{2}){0,1}$)

However I'm having trouble making the commas optional.

Upvotes: 3

Views: 5067

Answers (1)

hmakholm left over Monica
hmakholm left over Monica

Reputation: 23342

Um, just allow a number without commas as an explicit alternative:

^(\d?\d?\d(,\d\d\d)*|\d+)(\.\d\d)?$

Upvotes: 1

Related Questions