Regular Expression for 3 digit without 000

给你一囗甜甜゛ 提交于 2019-12-22 11:33:50

问题


I want to write a regular expression on Google Form

First Character between 1 to 9 Second and Third any alphabets (Upper Case) and next 3 characters should be number like 541 or 001 but not 000

This expression is also taking 000

[1-9][A-Z]{2}[0-9]{3}

回答1:


Use alternations:

[1-9][A-Z]{2}([1-9][0-9][0-9]|[0-9][1-9][0-9]|[0-9][0-9][1-9])

See regex demo

Here,

  • [1-9] - matches 1 digit from 1 to 9
  • [A-Z]{2} - two uppercase ASCII letters
  • ([1-9][0-9][0-9]|[0-9][1-9][0-9]|[0-9][0-9][1-9]) - 3 alternatives:
    • [1-9][0-9][0-9] - 3-digit numbers starting with 1
    • [0-9][1-9][0-9] - 3-digit numbers having 1 in the middle
    • [0-9][0-9][1-9] - 3-digit numbers ending with 1

Also, see this regex demo.




回答2:


Use a negative look-ahead to avoid the triple zero at the end:

[1-9][A-Z]{2}(?!000)[0-9]{3}



回答3:


Using the alternation operator [1-9][1-9][1-9]|0[1-9][1-9]|00[1-9]|0[1-9]0



来源:https://stackoverflow.com/questions/34697197/regular-expression-for-3-digit-without-000

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