正则表达式在字符类中无法按预期使用连字符
我有这段代码,但没有按我的预期工作。
如果我写 #$%
或 textwithouterrors
则显示的消息是“正确的”。所以,正则表达式不起作用。
我需要避免特殊字符、空格和数字
function validateCity($form) {
if (preg_match("/[\~!\@#\$\%\^\&*()_+\=-[]{}\\|\'\"\;\:\/\?.>\,\<`]/", $form['city'])) {
echo ("error");
return false;
} else {
echo("correct");
return true;
}
}
validateCity($form);
I have this code, but is not working as I expect.
If I write #$%
or textwithouterrors
the message showed is "correct". So, the regex is not working.
I need to avoid, special characters, spaces and numbers
function validateCity($form) {
if (preg_match("/[\~!\@#\$\%\^\&*()_+\=-[]{}\\|\'\"\;\:\/\?.>\,\<`]/", $form['city'])) {
echo ("error");
return false;
} else {
echo("correct");
return true;
}
}
validateCity($form);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
有很多问题 - 连字符
-
应该移到开头或结尾或转义,否则它将被视为指示范围。[]
必须转义。在像 http://gskinner.com/RegExr/ 这样的地方尝试一下你想要的东西
你也包括很多里面的东西。只需使用类似
\w+
的内容作为有效匹配,而不是寻找无效匹配。There are lots of problems - The hypen
-
should be moved to first or end or escaped otherwise it will be seen as indicating a range. The[]
have to be escaped.Try out what you want in some place like http://gskinner.com/RegExr/
Also you are including lot of stuff in it. Just use something like
\w+
as the match for a valid one rather than looking for an invalid one.尝试扭转你的逻辑。寻找您想要的角色,而不是您不想要的角色。
Try reversing your logic. Look for the characters you want, not the ones you don't want.
这里有几个问题。最严重的一个是你的正则表达式中有语法错误:到目前为止,我注意到
[
、]
和-
全部未转义在你的角色类别中。我有点惊讶正则表达式引擎没有从这些错误中出错,因为它们在技术上会导致未定义的行为,但 PHP 往往对此类事情相当宽容。不管怎样,它并没有按照你的想法去做。在担心这个问题之前,先解决第二个问题:您将字符列入黑名单,但您应该使用白名单。这将大大简化您的模式,并且您不必担心像 ▲ 这样的疯狂字符会滑过您的正则表达式。
如果你想匹配城市,我会选择这样的东西:
这将允许字母、破折号(想想北卡罗来纳州温斯顿塞勒姆)和空格(想想康涅狄格州纽黑文),同时阻止其他所有内容。我不知道,这可能限制太多了;有谁知道名字里有数字的城镇的,欢迎评论。但是,
\p{L}
应匹配 unicode 字母,因此 Āhualoa, HI 应该有效。There are a couple of issues going on here. The most serious one is that you have syntax errors in your regex: So far, I've noticed
[
,]
, and-
all unescaped in your character class. I'm a little surprised the regex engine isn't erroring out from those, since they technically lead to undefined behavior, but PHP tends to be pretty tolerant of such things. Either way, it isn't doing what you think it is.Before worrying about that, address the second issue: You're blacklisting characters, but you should just use a whitelist instead. That will simplify your pattern considerably, and you won't have to worry about crazy characters like ▲ slipping past your regex.
If you're trying to match cities, I'd go with something like this:
That will allow letters, dashes (think Winston-Salem, NC), and whitespace (think New Haven, CT), while blocking everything else. This might be too restrictive, I don't know; anyone who knows of a town with a number in the name is welcome to comment. However, the
\p{L}
should match unicode letters, so Āhualoa, HI should work.您似乎想检查您的城市名称是否包含任何非字母字符。在这种情况下,您可以将其简化为:
It seems like you want to check if your city name contains any non-letter characters. In that case you can simplify it to:
该字符集有一个未转义的“]”,看起来像是该集的结尾。
The character set has an unescaped "]", which looks like the end of the set.
您需要在字符类内部转义
[
和]
。You need to escape
[
and]
inside of your character class.