Skip to content

Instantly share code, notes, and snippets.

@Dan1el42
Created September 15, 2016 19:34
Show Gist options
  • Save Dan1el42/ddf9c755f591466ce86a860f25532f62 to your computer and use it in GitHub Desktop.
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/
$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