For my purposes I just want a pattern matching algorithm to determine if an email address is correctly formatted according to the rules. I found this which outlines those rules:
https://help.returnpath.com/hc/en-us/articles/220560587-What-are-the-rules-for-email-address-syntax-
If these are correct, then it means a simple use of the ? pattern match is probably not going to be sufficient. Here's my somewhat verbose logic with some tests. Any suggestions/improvements/corrections??
replace(string,substr,to) ;
i $zv["GT.M" QUIT $$^%MPIECE(string,substr,to)
QUIT $replace(string,substr,to)
;
isValidEmail(email) ;
;
n chk,domain,dupFound,i,name,specialChars
;
; just a single @ ?
i $l(email,"@")'=2 QUIT 0
;
s name=$p(email,"@",1)
; missing name?
i name="" QUIT 0
; starts or ends with a letter or number?
i $e(name,1)'?1AN QUIT 0
i $e(name,$l(name))'?1AN QUIT 0
; duplicated special characters?
s specialChars=".!#$%&'*+-/=?^_`{|"
s dupFound=0
f i=1:1:$l(specialChars) d q:dupFound
. n s,ss
. s s=$e(specialChars,i)
. s ss=s_s
. s chk=$$replace(name,ss,s)
. i chk'=name s dupFound=1
i dupFound QUIT 0
;
; any other character than alphas, numbers or special characters?
s chk=$tr(name,specialChars,"")
i chk'?1AN.AN QUIT 0
;
s domain=$p(email,"@",2)
i $e(domain,1)'?1AN QUIT 0
i $e(domain,$l(domain))'?1AN QUIT 0
; missing top-level domain?
i $l(domain,".")<2 QUIT 0
; missing domain name
i $p(domain,".",1)="" QUIT 0
; missing intermediate domain parts
i $$replace(domain,"..",".")'=domain QUIT 0
; invalid characters in domain name?
s chk=$tr(domain,".-","")
i chk'?1AN.AN QUIT 0
; looks like it's formatted OK
QUIT 1
;
incorrectTest(email)
i $$isValidEmail(email) d
. w email_" was invalid but passed",!
e d
. w email_" was correctly rejected",!
QUIT
;
correctTest(email)
i $$isValidEmail(email) d
. w email_" passed OK",!
e d
. w email_" was OK but failed",!
QUIT
;
emailTests ;
;
d correctTest("
john.doe@gmail.com")
d correctTest("
john.doe43@domainsample.co.uk")
d incorrectTest("
.doe@gmail.com")
d incorrectTest("@domainsample.com")
d incorrectTest("johndoedomainsample.com")
d incorrectTest("john.doe@.net")
d incorrectTest("john.doe43@domainsample")
d correctTest("john.do%
e43@domainsample.com")
d incorrectTest("john.do%%
e43@domainsample.com")
d incorrectTest("john.do%e43@domainsample..com")
d incorrectTest("john.do%
e43@domainsample.com.")
d incorrectTest("john.do(
e43@domainsample.com")
d incorrectTest("john.doe43@domai%nsample.com")
;
QUIT
;
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)