Search code examples
regexregex-negationregex-lookaroundsregex-groupregex-greedy

RegEx for matching numbers from 0.00 to 50.00


How can I get an regex expression that allows:

0.00 to 50.00 and also comma as decimal separator so 0,00 to 50,00

I have gotten as far as ([0-5]{1})?([0-9]{1})([,][0-9]{1,2})? but there are still situations in which it fails. I have searched on line but could not find the right answer.

ADDED: A small change in requirements. Actually it should be from 0.01 to 50.00, and 0,01 to 50,00. (But with the answers below I think I managed to adapt the regex strings so this is also matched)


Solution

  • There is a regex for range generator here. Generated and played with it, came up with this

    ^(?:[1-4]?\d[.,]\d\d?|50[.,]00?)$
    

    Added non capturing group, little modifications and ^ start / $ end anchor. See demo at regex101.
    For optional decimal part, how about this: ^(?:[1-4]?\d(?:[.,]\d\d?)?|50(?:[.,]00?)?)$