Regular expression used for letters not failing for dots

#!/bin/bash
filename=$1
while read first last email
do
#echo "$first" "$last" "$email"
re='[a-zA-Z]+'
if [[ "$first" =~ $re && "$last" =~ $re ]]; then
        echo "$email"
else
        echo "bad"
fi
done < "$filename"

I am writing a bash script and trying to check the format list provided in the argument of the shell filename.File has the content as

Noble Able aable@ablecorp.com
Bob builder <bob_baker@bakerandsons.com>
Chris Charley  charley@bigcorp.com
Doug_douglas@maingroup.east.com
Ed Edwards edwards@inl.net.gov
kola.coe.edu Kola Jones

As what I expect it should give error(print bad) at the last line kola.coe.edu since it has the dot character but my result is

aable@ablecorp.com
<bob_baker@bakerandsons.com>
charley@bigcorp.com
bad
edwards@inl.net.gov
Jones

Answers


Regular expressions in the =~ construct are not anchored (unlike patterns which might have confused you: [[ $first == +([a-zA-Z]) ]]).

Regex matches even if a substring of the string matches it. You need to use ^ and $ for the beginning and end of the string, respectively.

[[ $first =~ ^[a-zA-Z]+$ ]]

Need Your Help

Is there a good way to deal with form submission in Scalatra

scala scalatra

Right now I'm just using params function to get the data that has been posted to an URL.

Android NDK: Multiple definition of .o and .c files

android c++ android-studio android-ndk java-native-interface

i want to implement a library with java and native code in my app project in Android Studio. Everytime when I change the c++ code, I have to recompile the c++ files so that the changes take effect ...