Skip to content

Instantly share code, notes, and snippets.

@MatthewJDavis
Last active March 3, 2021 00:24
Show Gist options
  • Save MatthewJDavis/74743afd13e54afd171289e1c4f70a3d to your computer and use it in GitHub Desktop.
Save MatthewJDavis/74743afd13e54afd171289e1c4f70a3d to your computer and use it in GitHub Desktop.
Use regex to extract an email address from a sting in PowerShell
# Extract just the email address of user from string.
$regex = "[a-z0-9!#\$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#\$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
$userList = 'Mick Jagger <[email protected]>', 'Keith Richards [email protected]', 'Ronnie W [[email protected]]'
$emailList = [System.Collections.Generic.List[string]]::new()
foreach ($user in $userList) {
$user -match $regex | Out-Null
$emailList.Add($Matches.values)
}
$emailList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment