Bash脚本,如何删除尾随子字符串,不区分大小写?

发布于 2024-10-05 03:43:58 字数 287 浏览 1 评论 0原文

我正在修改读取用户电子邮件的脚本。很简单,太简单了。

echo -n "Please enter your example.com email address: "
read email
email=${email%%@example.com} # removes trailing @example.com from email
echo "email is $email"

这有效,但仅适用于小写@example.com。我如何修改它以删除尾随的@example.com(不区分大小写)?

I am modifying a script that reads in a user email. It is very simple, too simple.

echo -n "Please enter your example.com email address: "
read email
email=${email%%@example.com} # removes trailing @example.com from email
echo "email is $email"

This works, but only for lower case @example.com. How could I modify this to remove the trailing @example.com, case insensitive?

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

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

发布评论

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

评论(6

臻嫒无言 2024-10-12 03:43:59

使用不区分大小写的正则表达式匹配:

echo -n "Please enter your example.com email address: "
read email
shopt -s nocasematch # Makes comparisons case insensitive
if [[ $email =~ example.com$ ]]; then
    domain=${BASH_REMATCH[0]}
    echo "email is ${email%%@$domain}"
fi

或者,您可以根据字符串的长度截断字符串(只有当您知道用户正确输入域时,这才有意义):

echo -n "Please enter your example.com email address: "
read email
domain=example.com
email_length=${#email}
domain_length=${#domain}
name_length=$(($email_length-$domain_length-1))
email=${email:0:$name_length} # removes trailing @example.com from email
echo "email is $email"

Use case insensitive matching to a regular expression:

echo -n "Please enter your example.com email address: "
read email
shopt -s nocasematch # Makes comparisons case insensitive
if [[ $email =~ example.com$ ]]; then
    domain=${BASH_REMATCH[0]}
    echo "email is ${email%%@$domain}"
fi

Alternatively, you can cut off a string based on its length (which only makes sense if you know that the user entered the domain correctly):

echo -n "Please enter your example.com email address: "
read email
domain=example.com
email_length=${#email}
domain_length=${#domain}
name_length=$(($email_length-$domain_length-1))
email=${email:0:$name_length} # removes trailing @example.com from email
echo "email is $email"
宣告ˉ结束 2024-10-12 03:43:58

如果您有 bash 4:

email=${email,,}
email=${email%%@example.com}

否则,也许只需使用 tr:

email=$(echo "${email}" | tr "A-Z" "a-z")
email=${email%%@example.com}

更新

如果您只是想剥离主机(任何主机),那么也许这确实是您想要的:

email=${email%%@*}

If you have bash 4:

email=${email,,}
email=${email%%@example.com}

Otherwise, perhaps just use tr:

email=$(echo "${email}" | tr "A-Z" "a-z")
email=${email%%@example.com}

Update:

If you are just wanting to strip the host (any host) then perhaps this is really what you want:

email=${email%%@*}
巡山小妖精 2024-10-12 03:43:58

对于 Bash 3.2 及更高版本:

shopt -s nocasematch
email='[email protected]'
pattern='^(.*)@example.com

[[ $email =~ $pattern ]]
email=${BASH_REMATCH[1]}    # result: JoHnDoE

For Bash 3.2 and greater:

shopt -s nocasematch
email='[email protected]'
pattern='^(.*)@example.com

[[ $email =~ $pattern ]]
email=${BASH_REMATCH[1]}    # result: JoHnDoE
-柠檬树下少年和吉他 2024-10-12 03:43:58

使用 sed 怎么样?

email="$(sed 's|@example\.com$||i' <<<"$email")"

请注意 sed 替换命令中的“i”标志,它请求不区分大小写的匹配。

How about using sed?

email="$(sed 's|@example\.com$||i' <<<"$email")"

Note the 'i' flag in the sed substitution command which requests case-insensitive matching.

养猫人 2024-10-12 03:43:58

这是另一个(虽然有点长)的看法:

email='[email protected]'
email=${email%%@[Ee][Xx][Aa][Mm][Pp][Ll][Ee].[Cc][Oo][Mm]}
echo "email is $email"

Here's yet another (though a bit lengthy) take on it:

email='[email protected]'
email=${email%%@[Ee][Xx][Aa][Mm][Pp][Ll][Ee].[Cc][Oo][Mm]}
echo "email is $email"
错々过的事 2024-10-12 03:43:58
echo -n "Please enter your example.com email address: "
read _email
_email=$(echo $_email | awk 'BEGIN{ FS="@" }  {print $1}')
echo $_email

要将电子邮件转为小写,您可以使用声明,例如

声明 -l email=$email

echo -n "Please enter your example.com email address: "
read _email
_email=$(echo $_email | awk 'BEGIN{ FS="@" }  {print $1}')
echo $_email

and for transfer email to lower case you can use declare, such as

declare -l email=$email

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