警告:preg_match() [function.preg-match]:未知修饰符 '(' in
当我尝试验证用户名时收到此错误消息。以下 pregexp 字符串是这样的: ^([a-zA-Z])[a-zA-Z_-]*[\w_-]*[\S]$|^([a-zA-Z] )[0-9_-]*[\S]$|^[a-zA-Z]*[\S]$^
。我已经尝试了我能想到的一切,但没有任何效果。
以下是我尝试过但失败的可能解决方案:
^([a-zA-Z])[a-zA-Z_-]*[\/w_-]*[\/S]$|^([a-zA-Z])[0-9_-]*[\/S]$|^[a-zA-Z]*[\/S]$^
/^([a-zA-Z])[a-zA-Z_-]*[\/w_-]*[\/S]$|^([a-zA-Z])[0-9_-]*[\/S]$|^[a-zA-Z]*[\/S]$^/
~^([a-zA-Z])[a-zA-Z_-]*[\/w_-]*[\/S]$|^([a-zA-Z])[0-9_-]*[\/S]$|^[a-zA-Z]*[\/S]$^~
#^([a-zA-Z])[a-zA-Z_-]*[\/w_-]*[\/S]$|^([a-zA-Z])[0-9_-]*[\/S]$|^[a-zA-Z]*[\/S]$^#
我希望你能帮助我。 提前致谢。
I get this error message when I trying to validate a username. The following pregexp string is this: ^([a-zA-Z])[a-zA-Z_-]*[\w_-]*[\S]$|^([a-zA-Z])[0-9_-]*[\S]$|^[a-zA-Z]*[\S]$^
. I have tried everything I can think of, but nothing works.
These are the following possible solutions I have tried, but fails:
^([a-zA-Z])[a-zA-Z_-]*[\/w_-]*[\/S]$|^([a-zA-Z])[0-9_-]*[\/S]$|^[a-zA-Z]*[\/S]$^
/^([a-zA-Z])[a-zA-Z_-]*[\/w_-]*[\/S]$|^([a-zA-Z])[0-9_-]*[\/S]$|^[a-zA-Z]*[\/S]$^/
~^([a-zA-Z])[a-zA-Z_-]*[\/w_-]*[\/S]$|^([a-zA-Z])[0-9_-]*[\/S]$|^[a-zA-Z]*[\/S]$^~
#^([a-zA-Z])[a-zA-Z_-]*[\/w_-]*[\/S]$|^([a-zA-Z])[0-9_-]*[\/S]$|^[a-zA-Z]*[\/S]$^#
I hope you can help me.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的正则表达式以
^
开头,因此这被解释为 分隔符。多个字符之后,您的正则表达式将在其中包含
^
,并且由于它是未转义的,因此它被解释为正则表达式的模式部分的结尾。紧接着第二个
^
之后,正则表达式将包含(
。在 preg_match 中,结束分隔符之后的任何内容都被解释为 修饰符,并且(
不是 修改。要修复它,您需要 的 PHP 手册页。
阅读有关分隔符 像这样的事情:
在您的第一个示例中,您似乎忘记添加任何分隔符,并且
^
并不是作为分隔符在您添加了分隔符的后续示例中。没有告诉我们这些有什么问题是(也许它们应该是不同的问题?)。Your regular expression starts with a
^
, so this is being interpreted as a delimiter.A number of characters later, your regular expression then contains a
^
within it, and since this is unescaped, it is being interpreted as the end of the pattern part of the regular expression.Immediately after the second
^
, your regular expression then contains a(
. In preg_match, anything after the closing delimiter is interpreted as a modifier, and(
is not a modifier.To fix it, you need to read the PHP manual page on delimiters.
I would assume you want something like this:
In your first example, it looks like you've forgotten to add any delimiters, and the
^
was not intended as a delimiter. In the subsequent examples where you have added delimiters, you haven't told us what the problems with those are (maybe they should be different questions?).您遇到的问题是
[\/S]
。这会强制用户名以S
或正斜杠/
结尾。您的意思是在那里写[\S]
。 (尽管我不确定这样做的目的是什么。这将允许末尾有任何非空格字符。)The problem you have is with
[\/S]
. This forces the usernames to end inS
or a forward/
slash. You meant to write[\S]
there. (Albeit I'm not sure what the purpose of that is. That would allow any non-space character at the end.)