Php正则表达式匹配包含下划线或后跟数字的字符串

时间:2013-06-05 06:06:15

标签: php regex string

我有这样的场景:

 a) word  => capture "word"
 b) word_and_digit_90 => capture "word_and_digit" and "90"
 c) word_90 => capture "word" and 90

我有这样的正则表达式对案例a)和c)有好处但是b)失败

 /([a-z]+)(?:_(\d+)){0,1}/i

如果有人建议a),b),c)

的解决方案,我感激不尽

修改

一个建议的解决方案是:

 /([a-z]+(?:_[a-z]+){0,})(?:_(\d+)){0,1}/i

我想如果有人建议好的替代方案

2 个答案:

答案 0 :(得分:4)

试试这个正则表达式:

/([a-z_]+[a-z])(?:_(\d+))?/

答案 1 :(得分:0)

最简单:

/^[a-z]+[_a-z\d]+$/