Created
September 15, 2016 19:34
-
-
Save Dan1el42/ddf9c755f591466ce86a860f25532f62 to your computer and use it in GitHub Desktop.
Regular expression example (Capturing groups) for https://powershell.org/forums/topic/regex-replace/
This file contains 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
$text = '[email protected]' | |
# Example 1: Unnamed capturing group | |
if ($text -match '@(.+)$') { | |
# Output the complete match | |
$Matches[0] # @example.com | |
# Output only the value of the capturing group | |
$Matches[1] # example.com | |
} | |
# Example 2: Named capturing group | |
if ($text -match '@(?<DomainName>.+)$') { | |
# Output only the value of the capturing group | |
$Matches.DomainName # example.com | |
} | |
# Example 3: | |
# Replace domain name and use a back reference to access | |
# the value of the capturing group with the mailbox/user name | |
$text -replace '^(.+)@.+$', '[email protected]' # Outputs: [email protected] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment