by JasonH at 2013-02-08 10:01:11
Hi all,by ArtB0514 at 2013-02-08 10:58:33
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!
Here’s a first try:by JasonH at 2013-02-08 14:18:16"'(\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).
Thank you for the example and explanation. I’ll give it a shot.by JasonH at 2013-02-11 15:45:49
Jason
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]}
}