0

For email validation, i have following conditions: 1. username should be alphanumeric only(a-z,0-9,A-Z) 2. website name should be a-z 3. extension should be a-z

i am writing a python code as :

import re 
def check(email):
    #complete the function
    #the function should return the strings "invalid" or "valid" based on the email ID entered
    if(re.search('^[a-zA-Z0-9]+$@^[a-z]+$.^[a-z]+$',email)):  
        return("valid")  

    else:  
        return("invalid") 


email=input()
print(check(email))

My input: vk@google.com But it returns me "Invalid" It should return as "Valid". What am i doing wrong here?

abc
  • 11,579
  • 2
  • 26
  • 51
Vandit
  • 109
  • 1
  • 7

2 Answers2

4

Your initial pattern will work with:

^[a-zA-Z0-9]+@[a-z]+\.[a-z]+$

Regular expression visualization

Debuggex Demo


You had: ^[a-zA-Z0-9]+$@^[a-z]+$.^[a-z]+$, where you had:

  • End string ancor $ midstring *2
  • Start string ancor ^ midstring *2
  • Unescaped . which would match any character. Escape with backslash > \.

Regular expression visualization

The above shows why you yield zero matches.


To have control over the amount of alpha characters between @ and the dot you could use:

^[a-zA-Z0-9]+@[a-z]{5,8}\.[a-z]+$
JvdV
  • 70,606
  • 8
  • 39
  • 70
  • Thanks JvdV. Couple of doubts...Why are we not using \ for unescaping @ ? Why it it only used for . Because '@' is also being used as a literal character here. And second, what if we want to set a limit to number of characters to be used between @ and . say 5-8 characters only between @ and . – Vandit Mar 01 '20 at 13:39
  • 1
    @Vandit. That is because the dot has a special meaning in RegEx and represents and character but newline. The @ has no special meaning. So only characters with special meaning need to be escaped with a backslash to match it literally. And to limit the amount you can say `{5,8}` meaning minimum of 5 and max of 8. I'll update the answer. – JvdV Mar 01 '20 at 13:49
  • @Vandit. Has this been helpfull and answered your question? If so, consider to click on the checkmark next to the answer to accept the answer and close the question. – JvdV Mar 13 '20 at 09:11
2

You can't expect the beginning of the line anywhere in the middle of the line.

                     This is not right
              |         |
'^[a-zA-Z0-9]+$@^[a-z]+$.^[a-z]+$'
                |        |
                       This is the problem

Also, the dot is not escaped, causing the expression to match any character. If you want it to match a literal dot, then you have to proceed it with a slash. Change that to

'^[a-zA-Z0-9]+@[a-z]+\.[a-z]+$'
Ahmad
  • 12,336
  • 6
  • 48
  • 88
  • There are more problematic ancors + an unescaped dot. =). Basically what I wrote down in my answer. – JvdV Mar 01 '20 at 09:22