仅验证带有Bash Regex的电子邮件地址用户名

发布于 2025-02-08 19:34:22 字数 1331 浏览 2 评论 0 原文

在 约翰逊按服务器/客户端标准有效(可能 rfc 5322 )电子邮件地址。也就是说,Gmail和Thunderbird会接受它们。

这个问题地址为完整的电子邮件地址,我不需要:我如何使用正则表达式验证电子邮件地址?

这个不受欢迎的问题是关于JavaScript的,没有答案:验证电子邮件地址的用户名

一部分-An-email-address-indress-in-a-rgular-expression“>上面的第一个问题为完整的电子邮件地址提供了一个可半手的正则 ,我不需要,尽管似乎可能有改进的余地,但我可能不需要改进: https:// stackoverflow。 COM/A/201378/10343144

我目前的最佳解决方案是采用:

emailusername="$1"
testemail="${emailusername}@nonsense.com"
regex='some existing full-email regex'
if [[ "${testemail}" =~ ${regex} ]]; then
  echo "it works"
fi

但是,这并不能专门针对电子邮件用户名部分,而且比仅验证用户名部分更昂贵。

是否在用户名中添加一个废话域,以检查最佳方法吗?

还是只能处理电子邮件地址的用户名部分?

In: [email protected]...

I need to make sure that 'johnson' is valid by server/client standards (probably RFC 5322) for the username part of an email address. That is, Gmail and Thunderbird would accept them.

This question addresses full email addresses, which I don't need: How can I validate an email address using a regular expression?

This unpopular question is about JavaScript and doesn't have answers: Validating username part of email address

This answer to the afirst question above offers a semi-acceptable regex for a full email address, which I don't need, though it seems there might be room for improvement, but I might not need improvement: https://stackoverflow.com/a/201378/10343144

My current best solution would be to take:

emailusername="$1"
testemail="${emailusername}@nonsense.com"
regex='some existing full-email regex'
if [[ "${testemail}" =~ ${regex} ]]; then
  echo "it works"
fi

But, that doesn't address the email username part specifically, and it's more expensive than validating only the username part.

Is adding a nonsense domain to the username for a regex check the best way?

Or is there a regex that can handle only only the username part of an email address?

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

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

发布评论

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

评论(2

楠木可依 2025-02-15 19:34:22

对于接受的用户名,Gmail比RFC更具限制性(参见创建一个用户名):

  • “滥用”和“邮政局长”是保留的
  • 6–30个字符长
  • 可以包含字母( az ),数字( 0-9 )和周期())
  • 不能包含 [...]
  • 可以以 [...] deveve ofde())
  • 开始或结束

  • 期间(点)在Gmail地址中无关紧要


备注:用户名的长度未考虑点。

然后,为了使用Bash验证Google用户名:

#!/bin/bash

username="$1"
username_nodots="${username//./}" 

if ! {
    (( ${#username_nodots} >=  6 )) && # this rule also excludes 'Abuse' 
    (( ${#username_nodots} <= 30 )) &&
    [[ $username =~ ^[[:alnum:]]+(\.[[:alnum:]]+)*$ ]] &&
    [[ $username != 'Postmaster' ]]
}
then
    echo "error: illegal google username: $username" >&2
    exit 1
fi

编辑:遵循@triperee建议,即使用标准壳构造:

username="$1"
length=$(printf %s "$username" | tr -d '.' | wc -c)

[ "$length" -ge 6 ] || {
    printf '%s\n' 'too short' >&2
    exit 1
}
[ "$length" -le 30 ] || {
    printf '%s\n' 'too long' >&2
    exit 1
}
case $username in
    *[^[:alnum:].]*)
        printf '%s\n' 'illegal character' >&2
        exit 1
    ;;
    .*)
        printf '%s\n' 'starts with dot' >&2
        exit 1
    ;;
    *.)
        printf '%s\n' 'ends with dot' >&2
        exit 1
    ;;
    *..*)
        printf '%s\n' 'multiple dots in a row' >&2
        exit 1
    ;;
    Abuse|Postmaster)
        printf '%s\n' 'reserved username' >&2
        exit 1
    ;;
esac

Gmail is more restrictive than the RFC in respect of the accepted usernames (see Create a username):

  • “Abuse” and “Postmaster” are reserved
  • 6–30 characters long
  • can contain letters (a-z), numbers (0-9), and periods (.)
  • cannot contain [...] more than one period (.) in a row
  • can begin or end with [...] except periods (.)
  • periods (dots) don’t matter in Gmail addresses

remark: the length of a username doesn't take the dots into account.

Then, for validating a Google username with bash you could do:

#!/bin/bash

username="$1"
username_nodots="${username//./}" 

if ! {
    (( ${#username_nodots} >=  6 )) && # this rule also excludes 'Abuse' 
    (( ${#username_nodots} <= 30 )) &&
    [[ $username =~ ^[[:alnum:]]+(\.[[:alnum:]]+)*$ ]] &&
    [[ $username != 'Postmaster' ]]
}
then
    echo "error: illegal google username: $username" >&2
    exit 1
fi

Edit: following @tripleee advice, i.e. using standard shell constructs:

username="$1"
length=$(printf %s "$username" | tr -d '.' | wc -c)

[ "$length" -ge 6 ] || {
    printf '%s\n' 'too short' >&2
    exit 1
}
[ "$length" -le 30 ] || {
    printf '%s\n' 'too long' >&2
    exit 1
}
case $username in
    *[^[:alnum:].]*)
        printf '%s\n' 'illegal character' >&2
        exit 1
    ;;
    .*)
        printf '%s\n' 'starts with dot' >&2
        exit 1
    ;;
    *.)
        printf '%s\n' 'ends with dot' >&2
        exit 1
    ;;
    *..*)
        printf '%s\n' 'multiple dots in a row' >&2
        exit 1
    ;;
    Abuse|Postmaster)
        printf '%s\n' 'reserved username' >&2
        exit 1
    ;;
esac
纵情客 2025-02-15 19:34:22

如果您的语言环境为 c 以下内容可能对您有用。它的灵感来自您提到的最后一条言论(尚未针对RFC检查),并且没有经过广泛的测试:

atext="A-Za-z0-9!#\$%&'*+/=?^_\`{|}~-"
qs1=
\x09\x0a\x0d\x20\x22\x5c\x80-\xff'
qs2=
\x0a\x0d\x80-\xff'
[[ "$localpart" =~ ^([$atext]+(\.[$atext]+)*|\"([^$qs1]|\\[^$qs2])*\")$ ]] && echo "yes"

If your locale is C the following may work for you. It is inspired by the last regex you mention (which has not been checked against the RFC), and was not extensively tested:

atext="A-Za-z0-9!#\$%&'*+/=?^_\`{|}~-"
qs1=
\x09\x0a\x0d\x20\x22\x5c\x80-\xff'
qs2=
\x0a\x0d\x80-\xff'
[[ "$localpart" =~ ^([$atext]+(\.[$atext]+)*|\"([^$qs1]|\\[^$qs2])*\")$ ]] && echo "yes"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文