PHP regular expression with optional underscore and number

谁都会走 提交于 2019-12-24 19:01:47

问题


I want a PHP regular expression that must start from 1 to 5 lower case characters, followed by an optional underscore, and then followed by 0 to 5 numbers/digits.

This is my code: '/[a-z][_][0-9]/'


回答1:


You're close. You need range and optional sigils:

/[a-z]{1,5}_?[0-9]{0,5}/

The expression {n,m} following a character class means the class must match at least n times and at most m times.

The expression ? following a character means the character must match 0 or 1 times. It's equivalent to {0,1}, just shorter.

If this doesn't work for your subject strings, please post some example matches and non-matches.



来源:https://stackoverflow.com/questions/59331689/php-regular-expression-with-optional-underscore-and-number

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