-3

I'm trying to create a program where it checks the validation of an email seeing if it starts with a lowercase letter, contains an "@" sign, and a ".com" or a ".co.uk". How would I do this.

My internet is being funny therefore I am not able to check the expressions online.

This is what I have so far :

def valid_email():
    email_address = input("Enter your email: ")
    valid = re.match("[a-z]",email_address)
    if valid:
        print("That looks OK")
    else:
        print("Invalid, must be lowercase and contain an '@' sign")
        valid_email()
valid_email()
Panda God
  • 11
  • 3
  • You are only looking for lowercase letters so I doubt you need worry about checking, you don't check for `@` or that the string ends in either .com or .co.uk – Padraic Cunningham Dec 08 '15 at 11:10
  • There are many many many existing email validation regular expressions out there - is there any specific reason you are wanting to roll your own? It's better not to try and reinvent the wheel in these instances. – Lix Dec 08 '15 at 11:10
  • This will help you - http://stackoverflow.com/questions/8022530/python-check-for-valid-email-address – Shekhar Samanta Dec 08 '15 at 11:14

3 Answers3

-1

You can write a regex such as valid = re.match(r"[a-z0-9]+@[a-z]+\.(com|co.uk)",email_address)

This will check if your email follows the pattern which you have mentioned.

For further validations such as starting with a lowercase letter, contains an "@" sign, and a ".com" or a ".co.uk" , you can put conditions like

     if '@' not in email_address:
       print "Must contain @" 

Likewise you can put the various conditions.

Rohan Amrute
  • 764
  • 1
  • 9
  • 23
  • You are assuming that localparts cannot contain dots, or plus signs, or dashes, or many other valid punctuation characters. This will fail even on a substantial number of common `gmail.com` addresses (because these often have a dot somewhere in the localpart). This also fails on many valid domain names -- even within the `.com` and `.co.uk` TLDs--, as well as anything with a subdomain. – tripleee Dec 08 '15 at 11:55
  • I know about all these considerations. I just gave answer to the above question pertaining to what he expects in the email address. – Rohan Amrute Dec 08 '15 at 13:53
-1

With that being said, your code does is it searches for any character between a-z and stops. That is, if an address is blah@foo.co.uk, your regex will start, see that there's a 'b' in the beginning which matches [a-z] and will stop.

Instead use '[a-z]+' This will keep on searching until it reaches .
@ symbol is mandatory so it should be placed as is. Now the regex becomes '[a-z]+@'
And now as for the validation of domain, use: '[\w.]+[a-z]+'
So your final regex statement(as per your requirement) becomes:
re.match(r'[a-z]+@[\w.]+[a-z]+')

But the above is very weak regex and doesn't account for _ or . that might oucer in the username field. I highly suggest you use the regex already available for email validation instead of trying to make your own.

Here's what a regex for full email validation would look like (for python):

r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)" (from http://emailregex.com/)

And here's the General Email Regex:

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
Jarwin
  • 1,045
  • 1
  • 9
  • 30
-2

Your regex expression contains following statements:

  • Just 1 "@" symbol (which can be written as like "@")
  • 1 or more "a-z" letters at the beginning (which can be written as [a-z]+)
  • anything until "@" symbol (which can be written as[^@]+)
  • 1 of ".com" or ".co.uk" (which can be written as .[^@]+)

So your regex expression should be like this:

valid = re.match(r"[a-z]+[^@]+?\.[^@]+", email_address)
cengineer
  • 1,362
  • 1
  • 11
  • 18