Python string filtering

I am making an algorithm for checking a string (via email) - for example, "E-mail addres is valid", but these are the rules. The first part of the email should be a line that has 1-8 characters (it can contain the alphabet, numbers, underscore [_] ... all the parts that the email contains), and after @ the second part of the email should have a line with 1- 12 characters (also containing all legal expressions), and it must end with a .com top-level domain

email = raw_input ("Enter the e-mail address:")
length = len (email)
if length > 20 
    print "Address is too long"
elif lenght < 7:
    print "Address is too short"  
if not email.endswith (".com"):   
    print "Address doesn't contain correct domain ending"   
try:
    first_part = len (splitting[0])
    second_part = len(splitting[1])  

    account = splitting[0]
    domain = splitting[1] 

    list = "abcdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_."

    for c in account: 
        if c not in list:
            print "Invalid char", "->", c,"<-", "in account name of e-mail"

    for c in domain:
        if c not in list:
            print "Invalid char", "->", c,"<-", "in domain name of  e-mail"

    if first_part == 0:
        print "You need at least 1 character before the @"
    elif first_part> 8:
        print "The first part is too long"
    if second_part == 4:
        print "You need at least 1 character after the @"
    elif second_part> 16:
        print "The second part is too long"
except IndexError:
        print ""The address must consist of 2 parts connected with symbol @,\
 and ending must be .com"

    if first_part > 0 and first_part < 8 and second_part >4 and second_part < 16:
       print "Valid e-mail address"
+5
source share
3 answers

If I understand well, everything works for you, except for the part of the search for invalid characters. It's true?

for? . :

account = splitting[0]
domain = splitting[1]

. . char , :

for c in account:
    if c not in "abcdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.":
        print "Invalid char", c, "in e-mail"

for c in domain:
    if c not in "abcdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.":
        print "Invalid char", c, "in e-mail"

( string.ascii_letters + string.digits +"._" "abcdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.", , ), .

+1

FTW!

import re

address = 'test@gmail.com'
if re.match(r'^[a-z0-9_]{1,8}@[a-z0-9_]{1,8}\.com$', address, re.IGNORECASE):
  print 'valid'
else:
  print 'invalid'

( ) r'^\w{1,8}@\w{1,8}\.com$'

, , :)

+3

.

def validEmail(email, domains):

    length = len(email)

    index = email.find('@')

    if len(email[0:index]) <= 8 and len(email[0:index]) > 0:
        dot = email.find('.')
        if (len(email[index + 1:]) - dot) <= 12:
            if index+1==dot:
                return False
            else:
                for i in domains:
                    if email[dot + 1:] == i:
                        return True
    return False

domains = ['com', 'org', 'co.uk']

email = raw_input ("Enter the e-mail address:")
print validEmail(email, domains)
+1

All Articles