File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ """ Mask an email address
2+ Search for email addresses in a string, extract the email addresses, mask them and re-insert them in the string.
3+
4+ Author: Musketeer Computing
5+ Created: 10th October 2022
6+ """
7+ import re
8+
9+
10+ def main ():
11+ sentence = input ("Enter a sentence" )
12+ emails = re .findall (r'[a-z0-9\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+' , sentence )
13+ if not emails :
14+ print (' No email address to mask' )
15+ exit ()
16+ d_emails = {}
17+ for email in emails :
18+ m_email = mask_email (email )
19+ if m_email == 'error' :
20+ print ('There was an error when masking the email. Stopping the program' )
21+ exit ()
22+ d_emails [email ] = m_email
23+ for k_email , v_email in d_emails .items ():
24+ sentence = re .sub (k_email , v_email , sentence )
25+ print (sentence )
26+
27+
28+ def mask_email (email ):
29+ lo = email .find ('@' )
30+ domain_extension = email .rfind ('.' )
31+ word_count = len (email )
32+ if lo > 0 :
33+ return "{0}#####{1}@{2}###{3}" .format (email [0 ],
34+ email [lo - 1 ],
35+ email [lo + 1 ],
36+ email [domain_extension :word_count ])
37+ else :
38+ return "error"
39+
40+
41+ if __name__ == "__main__" :
42+ main ()
You can’t perform that action at this time.
0 commit comments