PHP Regex Quantify

时间:2009-09-09 06:30:21

标签: php regex

此正则表达式仅允许使用字母数字字符和一个句点。我无法弄清楚如何设置量词来限制5到20之间的字符数。我在PHP中使用它。

/^([a-zA-Z0-9\.](?!\.)|[a-zA-Z0-9]*\.(?!\.))[a-zA-Z0-9]*$/

3 个答案:

答案 0 :(得分:3)

至少使用{ n , m }量词> n 且最多 m 重复。

答案 1 :(得分:0)

使用

/^[a-zA-Z0-9]{5, 20}$/

答案 2 :(得分:0)

/^ ---------> search starting at the beginning for
( ----------> begin group that consists of
?= ---------> looking ahead for
[^.]* ------> no periods any number of times
\.? --------> followed by a period optionally (?) then
[^.]* ------> no periods any number of times
$ ----------> search starting at the end of the string for contents of the group
) ----------> end group
[a-z0-9.] --> a letter number or period
{5,25} -----> five to twenty five times
$ ----------> search starting at the end of the string for the whole previous pattern
/i ---------> case insensitive

我在前瞻之后添加了问号以使其成为可选项。我是否像上面那样理解正则表达式?具体来说,我是否正确,因为前方结束时的美元符号会从字符串末尾开始寻找前瞻性?我从来没有见过任何美元符号,只是在正则表达式的最后。谢谢你的帮助。