Operating System - HP-UX
1829574 Members
4422 Online
109992 Solutions
New Discussion

regular expressions please help me

 
SOLVED
Go to solution
I.Delic
Super Advisor

regular expressions please help me

hi,

Can somebody help me with checking the format of the mail adress.
"@" character should be included in the mail adress.
For example:

User should type an valid mail adress.
name@domainname.com

thank you in advance

idriz


3 REPLIES 3
RAC_1
Honored Contributor

Re: regular expressions please help me

grep address

You something like this.
cat "address" | tr -d "@"
stat=$(echo $?)

if [ ${stat} -ne 0 ];then
echo "not a valid address"
exit 1
else
echo "Valiad from address"
exit 0
fi

Anil

There is no substitute to HARDWORK
Fred Ruffet
Honored Contributor
Solution

Re: regular expressions please help me

with perl :

#echo test | perl -e 'while () { chomp; ~ m/^.*@.*$/g; print "Not a valid address!\n" if (!pos); }'
Not a valid address!
#echo test@domain.com | perl -e 'while () { chomp; ~ m/^.*@.*$/g; print "Not a valid address!\n" if (!pos); }'
#

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
A. Clay Stephenson
Acclaimed Contributor

Re: regular expressions please help me

Something like this should work:

ADDRESS="cstephen@xyz.com"
echo "${ADDRESS}" | grep -E -q '^[a-zA-Z]+@[a-zA-Z0-9]$'
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "Pattern ok"
else
echo "Pattern bad"
fi

The -q put grep -E in quiet mode. We are only interested in the status. Change ${ADDRESSS} to a value without the @ and it should fail with ${STAT} being set to a non-zero value.

A more rigorous test would probably use awk to split the address into an array using the @ and then do an nslookup for an MX record.
If it ain't broke, I can fix that.