'Created a regex to capture the email address out of each line of text in the txt file. What would be a more universal way of writing this?

I am trying to capture the email addresses out of each line of text out of a txt file on regex101.com.

Capturing everything after the "@" symbol was easy enough, but I ran into trouble because some of the emails contain "." which is a special character in regex. I managed to capture the right stuff, but the expression looks very clunky and I was wondering what a more efficient way of writing would be.

Expression:

(\w*\d*\.?\w*\d*\.?\d*\w*@\w*\.(?:com|miami))$ 

Working txt file:

Jazmine Holcomb 3212 Adams Avenue Washington MD [email protected]
Sofie Hagan 4241 Jerry Dove Drive Erie PA [email protected]
Cairo Tyson 3768 Clifford Street San_Jose CA [email protected]
Tasmin Kearney 2956 Adams Drive El_Campo CA [email protected]
Aydin Moran 3727 Sarah Drive Lake_Charles LA [email protected]
Samirah Pollard 946 Douglas Dairy Road Prosperity SC [email protected]
Jaskaran Wheeler 1521 Richards Avenue Torrance CA [email protected]
Gerrard Browning 4690 Felosa Drive Los_Angeles CA [email protected]
Haleema Craft 73 Pinchalone Street Norfolk VA [email protected]
Brett Neal 4079 Johnson Street Garner NC [email protected]


Solution 1:[1]

You may use this regex instead:

\w[\w-]*(?:\.\w[\w-]*)*@\w+\.(?:com|miami)

RegEx Demo

Solution 2:[2]

\w[\w.]+\w@\w[\w.]*\.(com|miami)$ should match everything as well, as \w matches both digits and letters.

Anyway, if email contained characters different from letters, digits and underscore _ though, neither mine nor your regex will match.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 anubhava
Solution 2