0

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

For example, given:

$email = "take this email [email protected]"
$email2 = "[email protected]"
$email3 = "[email protected] needs extracted"

I would get:

[email protected]
[email protected]
[email protected]

The domain is the same among all these strings, it will always be an [email protected] address.

Any help is appreciated!

1

4 Answers 4

0

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

\[email protected]

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_.-][email protected]

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

Sign up to request clarification or add additional context in comments.

Comments

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 [email protected]"
$email2 = "[email protected]"
$email3 = "[email protected] needs extracted"
$email4 = "[email protected] and [email protected] and [email protected]"

$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

2 Comments

Becareful. You're including a " symbol in your regex causing the quotes to break. Using a back tick ` symbol should alleviate that issue
yes, i have there back tick, because i'm careful ;-)
0

Here's another way:

$email = "take this email [email protected]"
$email2 = "[email protected]"
$email3 = "[email protected] needs extracted"

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

foreach($a in $array){

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

    $email
}

Comments

0

This should be pretty simple in PS;

# initialize your string and pattern (you can choose your own pattern)
$message = "I'm a [email protected], I'm a [email protected]"
$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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.