0

The program should find all the emails from the string but it is returning nothing.

my_str="""
Practice @Geeksforgeeksexpand_more
Algorithmsexpand_more
Data Structuresexpand_more
Programming Languagesexpand_more
Web Technologiesexpand_more
Tutorial Libraryexpand_more
Computer Science Subjectsexpand_more
GATE 2021expand_more
UGC NET / ISROexpand_more
QUIZ Sectionexpand_more
Puzzles
Geeksforgeeks Initiativesexpand_more
Contact Us
Address:
GeeksforGeeks
5th & 6th Floor, Royal Kapsons, A- 118,
Sector- 136, Noida, Uttar Pradesh (201305)

For feedback and queries: feedback@geeksforgeeks.org

For course related queries: geeks.classes@geeksforgeeks.org
For payment related issues: geeks.classes@geeksforgeeks.org
For any issue in a purchased course : complaints@geeksforgeeks.org
To contribute, please see the contribute 

page"""

These lines of code should find all the emails-

pattern=re.compile(r'.*[a-z]@[a-z].[a-z]')
match=pattern.finditer(my_str)
for matches in match:
    print(matches)

Thank you.

Tanish Sarmah
  • 430
  • 5
  • 14

2 Answers2

1

Using findall():

import re

my_str="""
Practice @Geeksforgeeksexpand_more
Algorithmsexpand_more
Data Structuresexpand_more
Programming Languagesexpand_more
Web Technologiesexpand_more
Tutorial Libraryexpand_more
Computer Science Subjectsexpand_more
GATE 2021expand_more
UGC NET / ISROexpand_more
QUIZ Sectionexpand_more
Puzzles
Geeksforgeeks Initiativesexpand_more
Contact Us
Address:
GeeksforGeeks
5th & 6th Floor, Royal Kapsons, A- 118,
Sector- 136, Noida, Uttar Pradesh (201305)

For feedback and queries: feedback@geeksforgeeks.org

For course related queries: geeks.classes@geeksforgeeks.org
For payment related issues: geeks.classes@geeksforgeeks.org
For any issue in a purchased course : complaints@geeksforgeeks.org
To contribute, please see the contribute 

page"""

matches = re.findall(r'[\w\.-]+@[\w\.-]+', my_str)
print(matches)

OUTPUT:

['feedback@geeksforgeeks.org', 'geeks.classes@geeksforgeeks.org', 'geeks.classes@geeksforgeeks.org', 'complaints@geeksforgeeks.org']       
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • @TanishSarmah your regex doesn't match emails, your regex matched any succession of chars ended in a lowercase letter followed by `@`, another lowercase letter, a dot (`.`) and another lowercase letter, for example `safkhga4352dsf-y@a.a` as that ends in `y@a.a` – Adirio Sep 03 '20 at 09:45
  • 1
    Also mentioning that you use `findall()` is slightly confusing as it suggests that OP's use of `finditer()` was incorrect – 7koFnMiP Sep 03 '20 at 09:48
0

Your RegExp is totally wrong, that's the reason. Here is the barely corrected version:

\b[a-z.]+@[a-z]+\.[a-z]+

Fiddle: https://regex101.com/r/kNAbHZ/2

Output of your code becomes:

<re.Match object; span=(471, 497), match='feedback@geeksforgeeks.org'>
<re.Match object; span=(527, 558), match='geeks.classes@geeksforgeeks.org'>
<re.Match object; span=(587, 618), match='geeks.classes@geeksforgeeks.org'>
<re.Match object; span=(657, 685), match='complaints@geeksforgeeks.org'>

Note that finding a valid email address is much more complex than that. See https://www.regular-expressions.info/email.html.

ceremcem
  • 3,900
  • 4
  • 28
  • 66