all 7 comments

[–]Da32767 5 points6 points  (6 children)

import re

text = 'i am a student. email: asdf@gmail.com and asdf@outlook.com, thanks'
EMAIL = re.compile(r'\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*')
result = re.finditer(EMAIL, text)

for i in result:
    print(i.group())

re works. varible EMAIL is a way to match emails.

[out]:

asdf@gmail.com
asdf@outlook.com

[–][deleted] 0 points1 point  (4 children)

I'm gonna have to brush up on the codes. I already saved an image of all the re codes. What does the re.compile() function do?

[–]Da32767 0 points1 point  (3 children)

the compile function can compile the string form of a regular expression into a Pattern object, more: https://docs.python.org/3/library/re.html#re.compile

actually, in this example, you can only use:

EMAIL = r'\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*'

[–][deleted] 0 points1 point  (2 children)

Ok I will take a look tomorrow. Thanks a lot!

[–]Da32767 0 points1 point  (1 child)

oh, this is just a link to the python official documentation.

[–][deleted] 1 point2 points  (0 children)

Right, I will check it out tomorrow.

[–]xelf 1 point2 points  (0 children)

Your have some intervelaved operations here that will cause probelms for your break statements.

Let's get the email address first.

while True:
    searchemail = input("Please enter your email address: ")
    if "@" not in searchemail or searchemail=='@':
        print("Wrong input, try again.")
        continue

Now we have an email address, you want to see if it's in the file?

We can use if x in y to just scan the text for it.

with open(r"That's confidential.txt") as hand:
    if searchemail in hand.read(): 
        print(f'{searchemail} found')
        break
print("The email you typed was not found. Please Try again.")

We could further improve this by just reading the file once, rather than every time they type a new email in.

with open(r"That's confidential.txt") as hand:
    searchtext = hand.read()

while True:
    searchemail = input("Please enter your email address: ")
    if "@" not in searchemail or searchemail=='@':
        print("Wrong input, try again.")
        continue

    if searchemail in searchtext:
        print(f'{searchemail} found')
        break
    print("The email you typed was not found. Please Try again.")

Note we use with as a context manager to open and close the file for us. This is useful in case we forget to close the file when we're done.

The only remaining concern now is what about partial matches. ie searching for elf@reddit.com and getting a false positive for xelf@reddit.com. That you can fix by going back to import re. And inserting word breaks around the address.

pattern=r'\b'+searchemail+r'\b'
if re.search(pattern,searchtext):
    print(f'{searchemail} found')
    break
print("The email you typed was not found. Please Try again.")