电子邮件定期检查前pression与bash脚本 [英] Email check regular expression with bash script
问题描述
我尝试做一个bash脚本来检查的电子邮件地址是正确的。
我有这样的规律前pression:
<$p$p><$c$c>[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?来源: HTTP://www.regular-ex$p$pssions。信息/ email.html
这是我的bash脚本:
<$p$p><$c$c>regex=[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?I =test@terra.es如果[[$ i =〜$正则表达式]];然后
回声OK
其他
回声不OK
科幻
脚本失败,给我这样的输出:
10:语法错误:EOF在反引号替换
任何线索?
行情,反引号和其他人都在shell脚本特殊字符和需要转义如果他们在正则表达式的分配使用像
。你可以逃避特殊字符反斜杠,或者如果你离开了它使用的单引号周围使用正则表达式单引号。
我会建议使用一个简单的正前pression像。* @ *
,因为所有的复杂性是徒劳的。 foo@example.com
看起来完美的罚款,将由任何常规的前pression所接受,但仍然不存在。
im trying to make a bash script to check if a email address is correct.
I have this regular expression:
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
Source: http://www.regular-expressions.info/email.html
And this is my bash script:
regex=[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
i="test@terra.es"
if [[ $i=~$regex ]] ; then
echo "OK"
else
echo "not OK"
fi
The script fails and give me this output: 10: Syntax error: EOF in backquote substitution
any clue??
Quotes, backticks and others are special characters in shell scripts and need to be escaped if they are used like in the assignment of regex
. You can escape special characters with backslashes, or use single quotes around the regex if you leave out the single quote used in it.
I would recommend to use a simpler regular expression like .*@.*
because all the complexity is futile. foo@example.com
looks perfectly fine and will be accepted by any regular expression, but still it doesn't exist.
这篇关于电子邮件定期检查前pression与bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!