Last active
April 2, 2021 20:37
-
-
Save bixb0012/1fa1cdc79e5ce92011d20e821b91076c to your computer and use it in GitHub Desktop.
PowerShell: Regular Expressions
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
#Requires -Version 5.1 | |
# Reference: 1) https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_regular_expressions | |
# Reference: 2) https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference | |
$TestEmailFrom = @( | |
"[email protected]" | |
" [email protected] " | |
"<[email protected]>" | |
" < [email protected] > " | |
"Smokey Bear <[email protected]>" | |
"Smokey Bear [email protected]" | |
"Smokey Bear <[email protected]>" | |
"Bear <[email protected]>" | |
"Bear<[email protected]>" | |
"Bear < [email protected] > " | |
"Smokey's Bear <smokey'[email protected]>" | |
"Smokey's Bear-Owl <smokey'[email protected]>" | |
"S. Bear <[email protected]>" | |
"S.S Bear <[email protected]>" | |
"Smokey Bear <[email protected]>" | |
" Smokey Bear <[email protected]>" | |
) | |
# Example 1: E-mail address validation and parsing | |
# Adapted from https://www.emailregex.com to add subaddressing tag | |
$EmailAddressRegex = @( | |
"^\s*" # BOL | |
"(?<LocalPart>\w+(?:[._\-']\w+)*)+" # local-part | |
"(?:\+(?<Tag>\w+(?:[._\-']\w+)*))?" # subaddressing tag | |
"@(?<Domain>(?:\w+(?:[-.]\w+)*)+\.(?:\w{2,}))" # domain | |
"\s*$" # EOL | |
) | |
# Example 2: E-mail From (name and address) validation and parsing | |
$EmailFromRegex = @( | |
"^\s*" # BOL | |
"(?:(?<FirstName>\w+(?:[.\-'][\s\w]+?)*?)\s*" # first name | |
"(?<LastName>\w+(?:[.\-'][\s\w]+?)*?)?\s*)??" # last name | |
"<?\s*?(?<LocalPart>\w+(?:[._\-']\w+)*)+" # local-part | |
"(?:\+(?<Tag>\w+(?:[._\-']\w+)*))?" # subaddressing tag | |
"@(?<Domain>(?:\w+(?:[-.]\w+)*)+\.(?:\w{2,}))+\s*?>?" # domain | |
"\s*$" # EOL | |
) | |
# Example 3: Phone number validation and parsing, North American Numbering Plan | |
# Adapted from Francis Gagnon at https://stackoverflow.com/questions/16699007/regular-expression-to-match-standard-10-digit-phone-number | |
$PhoneNanpRegex = @( | |
"^\s*" # BOL | |
"(?:\+?(?<CountryCode>\d{1,3}))?[-. (]*" # country code | |
"(?<AreaCode>\d{3})[-. )]*" # area code | |
"(?<Prefix>\d{3})[-. ]*" # central office code/prefix | |
"(?<LineNumber>\d{4})" # line number | |
"(?: *x(?<Extension>\d+))?" # extension | |
"\s*$" # EOL | |
) | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment