파이썬에서 re 모듈의 search() 함수를 사용해서 이메일 주소를 체크하는 코드를 짜줘
SubjectOwner
2024. 1. 9. 16:07
import re
def is_valid_email(email):
# Define a regular expression pattern for a valid email address
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
# Use the search() function to find a match in the email string
match = re.search(pattern, email)
# If a match is found, it's a valid email address
if match:
return True
else:
return False
# Example usage:
email = "example@email.com"
if is_valid_email(email):
print(f"{email} is a valid email address.")
else:
print(f"{email} is not a valid email address.")