Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i want to print all the valid email ids which are ending with @gmail.com from a file named mbox.txt
i tried to do it in the following way but getting the output wrong.How do i correct it?

What I have tried:

Python


import re

def validitycheck():
        fhandle = open(r"C:\\Users\\Aishwarya\Desktop\\temp.txt")
        for line in fhandle:
          if line.endswith("@gmail.com"):
            print (line)
            line = mailid
            pattern = re.compile(r"[\w.-]+@[\w.-]+.\w+@gmail.com")
            matchobject = pattern.match(mailid)
            
            if(matchobject):
                print("your mail id",line,"is valid")
            else:
                print("your mail id is invalid")
          else:
             print("no mail id as such")

validitycheck()
Posted
Updated 31-May-18 6:17am

1 solution

Why does your regex contain two '@' characters? It's legal, but only within a quoted string as part of the local-string: Email address - Wikipedia[^]
Isn't
[\w.-]+@gmail\.com
more appropriate?
 
Share this answer
 
v2
Comments
Member 13853236 31-May-18 13:45pm    
i changed the code into this:


import re
fhandle = open(r"C:\\Users\\Aishwarya\Desktop\\aish.txt")
def validitycheck():
for line in fhandle:
mylist = line.split()
print(mylist)


pattern = re.compile("\w*|\d+@gmail.com")
matchobject = pattern.match(mylist)
if (matchobject):
print("all the mail ids are:",mylist)




validitycheck()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900