Extracting email address from string. Regex likely.

by JasonH at 2013-02-08 10:01:11

Hi all,
I’m trying to extract email addresses from multiple lines in a text file. I’m thinking a regular expression will be required but I have some learning disability with regex and it just never makes sense. An exact sample line from the text file is:

439 Feb 6 2013 16:49:57: RUN29: ACTION: DELETE, Removed ‘(Jason Hall) soupjh@gmail.com’ from list

My hope is to get the email address from each line. Any suggestions? Thanks!
by ArtB0514 at 2013-02-08 10:58:33
Here’s a first try:
"’(\w+@\w+’"
Matches a single quote followed by a “(”, 1 or more “word” characters [a-zA-Z0-9], an “@” sign, 1 or more word characters, and ending with another single quote.
(I’m not sure that the “’” needs to be escaped, but it shouldn’t hurt).
by JasonH at 2013-02-08 14:18:16
Thank you for the example and explanation. I’ll give it a shot.
Jason
by JasonH at 2013-02-11 15:45:49
To follow up, after some playing around I used a regular expression from this page and ended up with this to get what I needed.

## regex pattern for email addresses
$pattern = “\b[A-Z0-9._%±]+@[A-Z0-9.-]+.[A-Z]{2,4}\b”

## Get content of txt file with multiple lines, each line has email address in it.
$lines = Get-Content c:\Temp\lines.txt

foreach ($line in $lines){
if ($line -match $pattern) {$matches[0]}
}