简体   繁体   中英

Python simple email validity check

In Python, if i wanted to write something that returns a boolean True as long as the @ symbol appears after the . symbol, how would I write this?

I have tried writing something like:

if myString[x] == "@" > myString[x] == ".":
     return True

but, I assume that this won't work because they're both essentially the same index. Looking for some explanation as well, just learning a lot of basics still. Thanks!

if str.index('@') > str.index('.'):
   return True;

You could use a simple regular expression:

import re

rx = re.compile(r'\S+@\S+')

string = """This text contains an email@address.com somewhere"""

if rx.search(string):
    print("Somewhere in there is an email address")

See a demo on ideone.com .

return str.find('@') > str.find('.') and str.find('.') > -1

This will check that @ is after . , and that both symbols are present in the string. This is however a very bad way to check for email validity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM