Data-Science-Regular-Bootcamp
Data-Science-Regular-Bootcamp copied to clipboard
Checking for valid email addresses
pattern = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
str_true = ('[email protected]',)
str_false = ('testmail.com', '@testmail.com', 'test@mailcom')
for t in str_true:
assert(bool(re.match(pattern, t)) == True), '%s is not True' %t
for f in str_false:
assert(bool(re.match(pattern, f)) == False), '%s is not False' %f
if you add the library import re then the code will run perfectly fine there is an import statement missing
the import re module provides regular expression matching operations similar to those found in Perl. It supports both 8-bit and Unicode strings; both the pattern and the strings being processed can contain null bytes and characters outside the US ASCII range.
import re pattern = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$)"
str_true = ('[email protected]',)
str_false = ('testmail.com', '@testmail.com', 'test@mailcom')
for t in str_true: assert(bool(re.match(pattern, t)) == True), '%s is not True' %t for f in str_false: assert(bool(re.match(pattern, f)) == False), '%s is not False' %f