正则表达式在字符类中无法按预期使用连字符

发布于 2024-11-11 15:18:02 字数 430 浏览 3 评论 0原文

我有这段代码,但没有按我的预期工作。

如果我写 #$%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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

鯉魚旗 2024-11-18 15:18:02

有很多问题 - 连字符 - 应该移到开头或结尾或转义,否则它将被视为指示范围。 [] 必须转义。

在像 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.

溺渁∝ 2024-11-18 15:18:02

尝试扭转你的逻辑。寻找您想要的角色,而不是您不想要的角色。

Try reversing your logic. Look for the characters you want, not the ones you don't want.

败给现实 2024-11-18 15:18:02

这里有几个问题。最严重的一个是你的正则表达式中有语法错误:到目前为止,我注意到 []- 全部未转义在你的角色类别中。我有点惊讶正则表达式引擎没有从这些错误中出错,因为它们在技术上会导致未定义的行为,但 PHP 往往对此类事情相当宽容。不管怎样,它并没有按照你的想法去做。

在担心这个问题之前,先解决第二个问题:您将字符列入黑名单,但您应该使用白名单。这将大大简化您的模式,并且您不必担心像 ▲ 这样的疯狂字符会滑过您的正则表达式。

如果你想匹配城市,我会选择这样的东西:

if(preg_match("/[^\p{L}\s-]/", $form['city'])) {
    echo ("error");
    return false;
}
//etc...

这将允许字母、破折号(想想北卡罗来纳州温斯顿塞勒姆)和空格(想想康涅狄格州纽黑文),同时阻止其他所有内容。我不知道,这可能限制太多了;有谁知道名字里有数字的城镇的,欢迎评论。但是,\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:

if(preg_match("/[^\p{L}\s-]/", $form['city'])) {
    echo ("error");
    return false;
}
//etc...

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.

黎歌 2024-11-18 15:18:02

您似乎想检查您的城市名称是否包含任何非字母字符。在这种情况下,您可以将其简化为:

 if (preg_match("/[^A-Z]/i", $form['city'])) {

It seems like you want to check if your city name contains any non-letter characters. In that case you can simplify it to:

 if (preg_match("/[^A-Z]/i", $form['city'])) {
舂唻埖巳落 2024-11-18 15:18:02

该字符集有一个未转义的“]”,看起来像是该集的结尾。

The character set has an unescaped "]", which looks like the end of the set.

对不⑦ 2024-11-18 15:18:02

您需要在字符类内部转义 []

You need to escape [ and ] inside of your character class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文