Regular expression allow zero so long as it is not the first digit [duplicate]

醉酒当歌 提交于 2019-12-11 01:07:54

问题


Yesterday I was posting one question here Regular Expression allow null or 1 to 9 digit This was working fine after that I saw in my quantity field not taking 10, 100, 1002 etc. value. because these values also present 0(zero) digit. So Please help me.


回答1:


If you want null or a number that starts from [1-9] but can contain [0-9], the pattern is

 (^[1-9][0-9]*$)|(^$)

please, notice that a single zero is not allowed: 0 doesn't match. In case you want to match it as well (you want a empty string, zero - 0 or a number which must start from [1-9] and can contain [0-9])

 (^[1-9][0-9]*$)|(^0?$)



回答2:


I would do with this expression:

(^[1-9][0-9]*$)|(^$)

or even shorter using \d which means [0-9]:

    (^[1-9]\d*$)|(^$)


来源:https://stackoverflow.com/questions/40377762/regular-expression-allow-zero-so-long-as-it-is-not-the-first-digit

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