0

I want to parse out email addresses from a string in PowerShell.

For example, given:

$email = "take this email test@mail.com"
$email2 = "secondmail@mail.com"
$email3 = "thirdmail@mail.com needs extracted"

I would get:

test@mail.com
secondmail@mail.com
thirdmail@mail.com

The domain is the same among all these strings, it will always be an username@mail.com address.

Any help is appreciated!

Cooper
  • 13
  • 1
  • 3
  • Possible duplicate of [Using Regex in Powershell to grab email](https://stackoverflow.com/questions/11564679/using-regex-in-powershell-to-grab-email) –  Oct 24 '18 at 21:25

4 Answers4

0

You can use this regex to extract email for your particular domain name.

\w+@mail.com

As long as username part in your email only contains alphabet, numbers and underscores. Else instead of \w you may have to write character class like this,

[a-zA-Z0-9_.-]+@mail.com

Like you can see this contains alphabets, numbers, undrscore, dot, hyphen

Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
0

Inspired by this answer

How to validate an email address using a regular expression?

and this answer

Powershell: Extract text from a string

this would help ....

$email = "take this email test@mail.com"
$email2 = "secondmail@mail.com"
$email3 = "thirdmail@mail.com needs extracted"
$email4 = "fourthmail@mail.com and fifthmail@mail.com and sixthmail@mail.com"

$email_regex = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|`"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*`")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])"

$extracted = [regex]::match($email, $email_regex).Value
$extracted

$extracted = [regex]::match($email2, $email_regex).Value
$extracted

$extracted = [regex]::match($email3, $email_regex).Value
$extracted

[regex]::matches($email4, $email_regex)|select value

the last line shows how to extract more email occurences from one text

Jirka Jr
  • 41
  • 4
0

Here's another way:

$email = "take this email test@mail.com"
$email2 = "secondmail@mail.com"
$email3 = "thirdmail@mail.com needs extracted"

$array = @($email,$email2,$email3)

foreach($a in $array){

    $split = $a.Split(" ")
    $email = ($split | ? {$_ -match "@"})

    $email
}
StevenMMK
  • 31
  • 6
0

This should be pretty simple in PS;

# initialize your string and pattern (you can choose your own pattern)
$message = "I'm a creep.1990_radio@gmail.com, I'm a wierdo_head@gmail.com"
$pattern = "([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})"

# get all the matches and store it in an array
$results = ($message | Select-String $pattern -AllMatches).Matches

# iterate over array to act on those items
foreach ($item in ($results)) { Write-Host $item.Value }

In last line, you could also use Write-Host $item to see available attributes.