Dec 21 2022 02:02 AM
Hi All,
I need help in powershell string replacement using regex.
I need to replace a decimal value after a specific string pattern with a new decimal value stored in a variable. Like, in the entire string, I need to replace all occurrences of ‘Version=xxx’ to ‘Version=$NewValue’. I tried the following but it didn’t work.
$New = $Orig.Replace(‘Version=(\d{1,3})’, ‘Version=$NewValue’)
$New = $Orig.Replace(‘Version=(\d*)’, ‘Version=$NewValue’)
Anyone has any suggestions?
Thanks
Nancy
Dec 21 2022 05:22 AM - edited Dec 21 2022 05:24 AM
$string = "This is a string with multiple Version=xxx values that need to be replaced."
$newValue = "3.14"
# Replace all occurrences of "Version=xxx" with the new value
$string -replace "Version=xxx", "Version=$newValue"
Got it from OpenAI's chatGBT :)
Dec 21 2022 11:51 AM
Hi @nancyvargas,
You can use one of the following methods
I hope this helps.
Regards,
Ruud