Regex Match decimal number between 0 to 1

▼魔方 西西 提交于 2021-02-11 11:17:24

问题


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 of 0 followed by one or digits

  • 1(\.0*)? matches 1 followed by a decimal point, and any number of 0s

  • The 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!