Warning: preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset


i'm trying to change the preg_match check from url checking to username checking which is min/max 2-16chrs, dash, space & hypen acceptable. i'm getting this error

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

Warning: preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 14

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

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

old code that looked for URL

寻找网址的旧代码

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

2 个解决方案

#1


8  

The problem is here:

问题出在这里:

[\w- ]{2,16}*

You can't use {2,16} and * together, you can only use one or the other.

你不能同时使用{2,16}和*,你只能使用其中一个。

If you were looking to match groups of 2 to 16 [\w- ]s, at least 0 times, wrap it in a subpattern and attach the * outside:

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

(?:[\w- ]{2,16})*

#2


0  

What BoltClock says is correct. But there are other problems with your regex as well. First, to solve your immediate problem, here's a regex, which validates a username to be from 2 to 16 characters long consisting only of letters, digits, underscores, dashes/hyphens and spaces:

BoltClock说的是正确的。但是你的正则表达式还有其他问题。首先,要解决您的直接问题,这里是一个正则表达式,它验证用户名长度为2到16个字符,仅包含字母,数字,下划线,短划线/连字符和空格:

if (preg_match('/^[A-Za-z0-9_\- ]{2,16}$/', $string)) {
    // Valid username.
}

Note that there is no need for the 'empty() ||' clause because the regex matches only if there are at least 2 chars.

请注意,不需要'empty()||'因为只有至少有2个字符才能匹配正则表达式。

Second, regexes are very useful (and can even be fun!), but if you are going to use them, you need to sit down and learn the syntax, plain and simple (its not that hard). I would strongly recommend spending an hour or two studying the basics. There is an excellent online tutorial at: www.regular-expressions.info. The time you spend there will pay for itself many times over. Happy regexing!

其次,正则表达式非常有用(甚至可以很有趣!),但是如果你打算使用它们,你需要坐下来学习语法,简单明了(并不那么难)。我强烈建议花一两个小时研究基础知识。有一个很棒的在线教程:www.regular-expressions.info。你在那里度过的时间会多次为自己付出代价。快乐regexing!

智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告