I'm trying to make a Bash script to check if an 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="[email protected]"
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??
Requirement 1: Email address field should mandate with “@” symbol and should have “. (dot)” at any place after @ symbol and can end with any type of domain. Example: [email protected] or [email protected] or [email protected] Below is the explanation for the above to be configured regular expression. we are going to use it on rule to validate email address.
If you only want to check if an address is grammatically correct then you could use a regular expression, but note that ""@ [] is a grammatically correct email address that certainly doesn't refer to an existing mailbox. The syntax of email addresses has been defined in various RFCs, most notably RFC 822 and RFC 5322.
Here are four regular expressions (often called regexes) that all validate the format of an email address. They have increasing degrees of complexity. The more complicated, the more accurate each is at matching only email addresses. 1. Dirt-simple approach Here's a regex that only requires a very basic [email protected]: Upside: Dirt simple.
This means that the regexes only match email addresses that are strictly RFC 5322 compliant. If you have to match "old" addresses (as the looser grammar including the "obs-" rules does), you can use one of the RFC 822 regexes from the previous paragraph.
You have several problems here:
^
and $
).?:
is not supported and needs to be removed.=~
operator.Final product:
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="[email protected]"
if [[ $i =~ $regex ]] ; then
echo "OK"
else
echo "not OK"
fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With