PHP正则表达式-{0,1},?等
正则表达式模式:PHP正则表达式-{0,1},?等,php,regex,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
[[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
(我意识到我还没有实现这里给出的代码中的repeat部分)
此正则表达式应该适用于您(假设X=8):
我想要[[1-9]]?给我(数字“1-9”和“-”)或空。目前它给了我(一个数字“1-9”和“-”)但不是空选项?倾向于匹配[1-9]和“-”的所有排列??比如:“2-”、“2-”、“-”等等?试过了。它似乎匹配整个内容或仅匹配前两个字符,即“2-”。相反,我需要选择“2-”的前两个字符实际上,当我在实际代码中尝试它时,它工作得很好。试一下:效果不好。。。我可能用错了?但是谢谢你。你能不能给我一个简短的说明为什么是([1-9])?而不是[[1-9]]?是的,因为
[[1-9]]?
是不正确的,因为这使得[[1-9]
是一个字符类,后跟literal-]
,而不是1-9
后跟literal-
。这将是:^([1-9]-)?[a-zA-Z1-9][a-zA-Z0-9]{6}([a-zA-Z0-Z0-9]){2}$>
^([1-9]-)?[a-zA-Z1-9][a-zA-Z0-9]{8}$