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?

Viewed 26

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 eluir.azevedo.7o@lucidmode.com
Sofie Hagan 4241 Jerry Dove Drive Erie PA zfraction00t@jfaccupuncture.com
Cairo Tyson 3768 Clifford Street San_Jose CA varifi.amin@disabilitylawyersatlanta.com
Tasmin Kearney 2956 Adams Drive El_Campo CA 1paul.clinssonq@aprilmovo.com
Aydin Moran 3727 Sarah Drive Lake_Charles LA 3egdol@blogspot.miami
Samirah Pollard 946 Douglas Dairy Road Prosperity SC rzekohahaha@gmailup.com
Jaskaran Wheeler 1521 Richards Avenue Torrance CA ardit.ditty@netfacc.com
Gerrard Browning 4690 Felosa Drive Los_Angeles CA cgbeshar@lotomoneymaker.com
Haleema Craft 73 Pinchalone Street Norfolk VA 0kjhdsxz123e@summitgg.com
Brett Neal 4079 Johnson Street Garner NC fali_gx2000w@plussmail.com
2 Answers

\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.

You may use this regex instead:

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

RegEx Demo

Related