Powershell - string replacement using regex

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?


Welcome to the forum. :wave:t4:

Instead of dot Net methods I’d recommend to use PowerShell operators. In this particular case the dot Net method .Replace() does not use regex. :wink:

This should do the trick:

$New = $Orig -replace '(?<=Version=)\d{1,3}', $NewValue

BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink: