mercredi 6 mai 2015

regular expression for decimal with fixed total number of digits

Is there a way to write regular expression that will match strings like

(0|[1-9][0-9]*)\.[0-9]+

but with a specified number of numeric characters. for example: for 3 numeric characters it should match "0.12", "12.3" but not match "1.234" or "1.2". I know I can write it something like

(?<![0-9])(([0-9]{1}\.[0-9]{2})|([1-9][0-9]{1})\.[0-9]{1})(?![0-9])

but that becomes quite tedious for large number of digits.

(I know I don't need {1} but it better explains what I'm doing)

Aucun commentaire:

Enregistrer un commentaire