1

I'm trying to learn regular expressions. How can I find the first occurrence of the email address given line below:

'   for somebody@domain.com  for   somebody@domain.com '

I was trying with occurrences as shown below:

et=re.findall('for (<.+@.+>){1}?','   for somebody@domain.com  for   somebody@domain.com ')

but without luck.

Progman
  • 16,827
  • 6
  • 33
  • 48
admfotad
  • 197
  • 3
  • 11

1 Answers1

1

If you ignore strict email address validation which is mentioned here , you can use :

import re

pattern = r'\S+@\S+'
string = '   for somebody@domain.com  for   somebody@domain.com '

try:
    first_match = next(re.finditer(pattern, string))
    print(first_match.group())
except StopIteration:
    print('No match found')

click for demo

S.B
  • 13,077
  • 10
  • 22
  • 49