Last active
March 3, 2021 00:24
-
-
Save MatthewJDavis/74743afd13e54afd171289e1c4f70a3d to your computer and use it in GitHub Desktop.
Use regex to extract an email address from a sting in PowerShell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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