字母数字的正则表达式

发布于 2024-08-23 11:37:11 字数 209 浏览 5 评论 0原文

我有这个 PHP 正则表达式:

$username = preg_replace('/[^a-z0-9]/i', '', $username);

它只允许 AZ0-9。如何同时允许 .-_

I've this PHP regular expression:

$username = preg_replace('/[^a-z0-9]/i', '', $username);

It allows only A-Z and 0-9. How can I allow ., - and _ as well?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

坚持沉默 2024-08-30 11:37:11

您可以使用以下正则表达式:

/[^a-z0-9._-]/i
  • 末尾的 i 是制作模式
    匹配不区分大小写。你可以
    删除它并使用: /[^a-zA-Z0-9._-]/
  • -(连字符)如果在 char 类中被包围,则在 char 类中具有特殊含义
    一边,所以我们把它放在最后,这样
    它按字面意思处理。您还可以
    执行: /[^a-z0-9.\-_]/ 我们在哪里
    转义连字符
  • char 类中的点不是元
    char 因此将按字面意思处理
    并且不需要逃避。

You can use the following regex:

/[^a-z0-9._-]/i
  • i at the end is to make the pattern
    matching case-insensitive. You can
    drop it and use: /[^a-zA-Z0-9._-]/
  • -(hyphen) has a special meaning in char class if its surrounded on both
    sided so we put it at the end so that
    its treated literally. You can also
    do: /[^a-z0-9.\-_]/ where we are
    escaping the hyphen
  • A dot in a char class is not a meta
    char hence will be treated literally
    and need not be escaped.
被你宠の有点坏 2024-08-30 11:37:11

很简单,只需将这些字符也添加到正则表达式中即可

$username = preg_replace('/[^a-zA-Z0-9._-]/','',$username)

。需要转义,因为它是“matchall”字符, - 放在最后,因为否则它将用于定义范围(我们当然可以转义它)。

Easy, just add those characters to the regular expression as well

$username = preg_replace('/[^a-zA-Z0-9._-]/','',$username)

The . needs to be escaped because its the 'matchall' character, the - goes in the end because otherwise it would be used to define a range (we could ofcourse have just escaped it).

迷爱 2024-08-30 11:37:11
$username = preg_replace('/[^a-z0-9._-]/i', '', $username);

删除 ([^]) 组中字符之一的任何内容。请注意,hypern 是最后一个,因此它失去了其特殊含义。

$username = preg_replace('/[^a-z0-9._-]/i', '', $username);

Removes anything that isn't ([^]) one of the characters on the group. Note the hypern is the last one there, so it loses its special meaning.

老子叫无熙 2024-08-30 11:37:11

如果您确切知道需要匹配什么,只需在字符组中指定它即可。破折号必须位于最开始或最后。

/[A-Z0-9._-]/

If you know exactly what you need to match, just specify it in a character group. The dash either needs to be at the very start or very end.

/[A-Z0-9._-]/
找回味觉 2024-08-30 11:37:11
$username = preg_replace('/[^a-z0-9._-]/i', '', $username);

您当前的代码实际上允许 AZaz - i 标志将您的正则表达式标记为不区分大小写。

$username = preg_replace('/[^a-z0-9._-]/i', '', $username);

Your current code actually does allow both A-Z and a-z - the i flag marks your regular expression as case-insensitive.

亣腦蒛氧 2024-08-30 11:37:11
$username = preg_replace('/[^a-z0-9._-]/i', '', $username);
$username = preg_replace('/[^a-z0-9._-]/i', '', $username);
-黛色若梦 2024-08-30 11:37:11

尝试

$username = preg_replace('/[^a-z0-9.-_]/i', '', $username);

Try

$username = preg_replace('/[^a-z0-9.-_]/i', '', $username);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文