警告:preg_match()[function.preg-match]:编译失败:无法在偏移处重复

acc*_*man 5 php regex preg-match

我正在尝试将urg检查中的preg_match检查更改为用户名检查,这是最小/最大2-16chrs,破折号,空格和可接受的.我收到了这个错误

警告: preg_match()[function.preg-match]:编译失败:在偏移量14处不重复

if(empty($string) || preg_match("#^([\w- ]{2,16}*(?:.[\w- ]{2,16}*)+):?(d+)?/?#i", $string))
Run Code Online (Sandbox Code Playgroud)

查找URL的旧代码

if(empty($string) || preg_match("#^(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/?#i", $string))
Run Code Online (Sandbox Code Playgroud)

Bol*_*ock 8

问题出在这里:

[\w- ]{2,16}*
Run Code Online (Sandbox Code Playgroud)

不能使用{2,16}*在一起,你只能使用一个或另一个.

如果您要匹配2到16 [\w- ]秒的组,至少0次,请将其包装在子模式中并附加*外部:

(?:[\w- ]{2,16})*
Run Code Online (Sandbox Code Playgroud)