String:
name@gmail.com
Checking for:
@
.com
My code
if [[ $word =~ "@" ]]
then
if [[ $word =~ ".com" || $word =~ ".ca" ]]
My problem
name@.com
The above example gets passed, which is not what I want. How do I check for characters (1 or more) between "@" and ".com"?
You can use a very very basic regex:
It looks for a string being exactly like this:
It can get as complicated as you want, see for example Email check regular expression with bash script.
See in action
why not go for other tools like perl:
The glob pattern would be:
[[ $word == ?*@?*.@(com|ca) ]]
?
matches any single character and*
matches zero or more characters@(p1|p2|p3|...)
is an extended globbing pattern that matches one of the given patterns. This requires:shopt -s extglob
testing: