I have a string of emails separated by comma and 1 space:
string_of_emails = "email1@company.com, email2@company.com, email3@company.com, ... , email999@company.com"
I want to run a validation test on that string that will make sure the string is really from the above format.
Meaning - check that EACH email is VALID (user@domain.com) + each email is separated by comma and 1 space + the last email shouldn't have a comma.
You can first convert the string to a list:
emails = string_of_emails .split(", ")
After that, you can either do your own regex check for each individual email, or use one of the many packages available to do this for you: Python Email Validator
for mail in emails:
# do your own regex check here
# OR
# Use the email validator like this
v = validate_email(email) # function from the email validator
Just want to share a rough idea...
import re
soe = "abc_123@123.com ,you@yahoo.com , we@gmail.co.uk, gmail.com, me@outlook.com ,"
soel = soe.split(',')
#first entry cannot have space
if soel[0].count(" ")!=0 :
print("Error\t\t:: First entry cannot contain space!")
#then, all subsequent must start with and contain exactly one space, along with a valid email
for email in soel[1:]:
if email.count(" ") > 1:
print("Invalid entry\t::" + email, ":: too many spaces")
continue
#simple email regex (with a single space in front)
match = re.search(r' ([\w\.-]+)@([\w\.-]+)', email)
if match == None:
print("Invalid entry\t::" + email + ":: make sure it follows the rule!")
else:
print("Valid entry\t::" + email)
or for more details,
import re
soe = " abc_123@123.com,you@yahoo.com , we@gmail.co.uk, gmail.com, me@outlook.com ,"
soel = soe.split(',')
end = len(soel)
if soel[-1].strip()=='':
print("Error:: Comma near the end of string!")
end -= 1
if soel[0].count(" ")>0:
print("Error:: First entry cannot contain space!")
for email in soel[1:end]:
if email.count(" ") != 1 :
print("Error:: " + email + " :: too many spaces!")
continue
if not email.startswith(" "):
print("Error:: " + email + " :: one space is needed after comma!")
continue
#simple email regex
match = re.search(r'([\w\.-]+)@([\w\.-]+)', email)
if match != None:
print("Correct format: " + match.group())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.