简体   繁体   中英

Php regex to match string containing underscore or followed by a digit

I have a scenario like this :

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

I have regexp like this which is good with case a) and c) but failing with b)

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

I appreciate if someone would suggest a solution for a), b), c)

edit

One suggested solution is :

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

I would like if someone suggest good alternative

试试这个正则表达式:

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

最简单的:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM