PHP正则表达式 - {0,1} ,?等

时间:2014-11-25 21:46:19

标签: php regex

正则表达式:

[[1-9]-]?[a-zA-Z1-9]

以下字符串是否匹配?:

2-
5-
2-S
5-t
S
t

我在这里不理解什么?


也试过:

[[1-9]-]{0,1}[a-zA-Z1-9]
[[1-9]-|][a-zA-Z1-9]
[[[1-9]-]|][a-zA-Z1-9]

目标

尝试提出以下模式:

1st character = Number 1-9
2nd character = always "-"
3rd character = Alphanumeric (all cases, 1-9)
4rd character = Alphanumeric (all cases, 0-9) Repeat X number of times
OR
1rd character = Alphanumeric (all cases, 1-9)
2rd character = Alphanumeric (all cases, 0-9) Repeat X number of times

(我意识到我还没有在这里给出的代码中实现重复部分)


1 个答案:

答案 0 :(得分:1)

这个正则表达式适合你(假设X = 8):

^([1-9]-)?[a-zA-Z1-9][a-zA-Z0-9]{8}$