问题
I want to match a decimal number between 0 and 1 (inclusive), but the regex I wrote will match anything more than 1 too: https://regex101.com/r/mN1iT5/3
How can I solve this
^[0-1]{1}(\.[0-9]{1,2})?$
回答1:
Do:
^(?:0*(?:\.\d+)?|1(\.0*)?)$
0*(?:\.\d+)?matches any number of0followed by one or digits1(\.0*)?matches1followed by a decimal point, and any number of0sThe above two are
|(OR-ed) to match any one of them
Demo
来源:https://stackoverflow.com/questions/40457957/regex-match-decimal-number-between-0-to-1