0

I have a problem where i need to search for email addresses in a long string. I need to return all the occurences of matches, and not only one.

I've tried:

def email():
    try:
        a = re.search(r'abc.{1,11}@xyz.co.za|[email protected]',string)
        return a.group(0)   
    except Exception:
        return 1

and

def email():
    try:
        a = re.search(r'abc.{1,11}@xyz.co.za' or '[email protected]',string)
        return a.group(0)   
    except Exception:
        return 1

and

def email():
    try:
        a = re.search('abc.{1,11}@xyz.co.za',string)
        b = re.search('[email protected]',string)
        return a.group(0),b.group(0)   
    except Exception:
        return 1

as well as other different combinations of this.

From the first script, I receive the first match of email address. From the second script I receive only one match From the third script i receive all the matches. My problem is that if one of them doesn't match, it returns 1.

What i would like to see is that it returns all the email addresses that matches, regardless of how many match.

4
  • Can't you just return a? Commented Apr 3, 2019 at 14:35
  • When I return a, it only matches on the first "match", and it will return <re.Match object; span=(4112, 4130), match ='[email protected]'> Commented Apr 3, 2019 at 14:37
  • Try r'(abc.{1,11}@xyz.co.za)|([email protected])' Commented Apr 3, 2019 at 14:40
  • Read the question is related you your concern stackoverflow.com/questions/30358266/… Commented Apr 3, 2019 at 14:40

1 Answer 1

0

I ended up doing it like this:

def mailbox():
   try:
        a = re.findall(r'abc.{1,11}@xyz.co.za|[email protected]',string)
        return set(a)  
    except Exception:
        return 1

The only problem is that it it will return set() if i don't match anything

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.