# Powershell - string replacement using regex

**URL:** <https://forums.powershell.org/t/powershell-string-replacement-using-regex/21204>\
**Category:** PowerShell Help\
**Created:** [December 21, 2022, 9:50am UTC](https://forums.powershell.org/t/powershell-string-replacement-using-regex/21204 "2022-12-21T09:50:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nancyvargas](https://avatars.discourse-cdn.com/v4/letter/n/74df32/32.png) [@nancyvargas](https://forums.powershell.org/u/nancyvargas)\
**Post date:** [December 21, 2022, 9:50am UTC](https://forums.powershell.org/t/powershell-string-replacement-using-regex/21204/1 "2022-12-21T09:50:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 21, 2022, 10:02am UTC](https://forums.powershell.org/t/powershell-string-replacement-using-regex/21204/2 "2022-12-21T10:02:23Z")

</div>

Nancy,  
Welcome to the forum. 👋🏽

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. 😉

This should do the trick:

```auto
$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](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/powershell-string-replacement-using-regex/21204/3 "2024-05-16T20:16:10Z")

</div>


