First time posting.
i have the following code to replace the suffix of an email and its working fine
replace all characters after @ sign with @testdomain.com
$a = 'john.doe@domain.com'
$b = $a -replace "[?=@].*", '@testdomain.com'
$b
john.doe@testdomain.com
what i would like to do, is to capture the actual left side ‘source’ regex expression to a variable, which would be @domain.com so that i know what i;m replacing and i don;t know how to do it.
Sorry if this had been posted before.
Thank you
>Solution :
So, I’m not sure if this is possible using only the -replace operator and without the use of -match which would store the capture group on the $Matches automatic variable.
This is how you could do it using the regex class directly:
$a = 'john.doe@domain.com'
$Capture = @{}
$b = [regex]::Replace($a, "[?=@].*", {
param($s)
'@testdomain.com'
$Capture.Value = $s.Value
})
$b # => john.doe@testdomain.com
$Capture.Value # => @domain.com