# request : find and replace using PowerShell (regex)

**URL:** <https://forums.powershell.org/t/request-find-and-replace-using-powershell-regex/14941>\
**Category:** PowerShell Help\
**Created:** [September 13, 2020, 7:57pm UTC](https://forums.powershell.org/t/request-find-and-replace-using-powershell-regex/14941 "2020-09-13T19:57:40Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![kucingkucing](https://avatars.discourse-cdn.com/v4/letter/k/6a8cbe/32.png) [@kucingkucing](https://forums.powershell.org/u/kucingkucing)\
**Post date:** [September 13, 2020, 7:57pm UTC](https://forums.powershell.org/t/request-find-and-replace-using-powershell-regex/14941/1 "2020-09-13T19:57:40Z")

</div>

hi, sorry i not native english speaker,

i have this example of a part of a js file, let said this text below is in random line, and lines total usually are 100 to 150:

```
{
    ID: Player,
    Lvl: 10,
    Atk: 30,
    Def: 50,
}
```

i like to find and replace only the Atk of the player to 999 using PowerShell batchfile,

the code i want is find:

```
ID: Player,
    Lvl: /*ignore this text*/,
    Atk: *,
```

then replace it with:

```
ID: Player,
    Lvl: /*ignore this text*/,
    Atk: 999,
```

because of this specific case, i am sorry if i asked the complete program,

i hope somebody can create for me or give me a clue how to create it,

thanks for read, have a nice day.

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [September 13, 2020, 9:51pm UTC](https://forums.powershell.org/t/request-find-and-replace-using-powershell-regex/14941/2 "2020-09-13T21:51:12Z")

</div>

Hopefully this example will help you.

```
$text = @'
{
    ID: Player,
    Lvl: 10,
    Atk: 30,
    Def: 50,
}
'@

$text -replace '(?<=Atk: )\d{2,3}','999'
```

Output

```
{
    ID: Player,
    Lvl: 10,
    Atk: 999,
    Def: 50,
}
```

Here is how it would look reading from a file.

```
$text = get-content c:\path\to\jsfile.js -raw

$text -replace '(?<=Atk: )\d{2,3}','999'
```

---

<div class="post-metadata">

**Author:** ![kucingkucing](https://avatars.discourse-cdn.com/v4/letter/k/6a8cbe/32.png) [@kucingkucing](https://forums.powershell.org/u/kucingkucing)\
**Post date:** [September 13, 2020, 11:47pm UTC](https://forums.powershell.org/t/request-find-and-replace-using-powershell-regex/14941/3 "2020-09-13T23:47:26Z")

</div>

thank you for the reply Doug Maurer,

i do not test it the code yet, because I don’t see the “keyword” of “Player” in your code, i assume another data who have “keyword” “Atk” will change to 999 too if i using your code, and i want to edit “Atk” only in “ID: Player” stats,

thank you again for your reply, i really appreciate it

&nbsp;

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [September 14, 2020, 5:02am UTC](https://forums.powershell.org/t/request-find-and-replace-using-powershell-regex/14941/4 "2020-09-14T05:02:03Z")

</div>

OK, perhaps this example will help more.

```
$text = @'
{
    ID: NotPlayer,
    Lvl: 10,
    Atk: 30,
    Def: 50,
}
{
    ID: Player,
    Lvl: 10,
    Atk: 30,
    Def: 50,
}
'@

$text -replace '(?s)(?<=ID: Player.*Atk: )\d{2,3}','999'
```

Output

```
{
    ID: NotPlayer,
    Lvl: 10,
    Atk: 30,
    Def: 50,
}
{
    ID: Player,
    Lvl: 10,
    Atk: 999,
    Def: 50,
}
```

---

<div class="post-metadata">

**Author:** ![kucingkucing](https://avatars.discourse-cdn.com/v4/letter/k/6a8cbe/32.png) [@kucingkucing](https://forums.powershell.org/u/kucingkucing)\
**Post date:** [September 14, 2020, 5:45am UTC](https://forums.powershell.org/t/request-find-and-replace-using-powershell-regex/14941/5 "2020-09-14T05:45:07Z")

</div>

thanks again for the reply,

i test your code like this :

(Get-Content c:\test.txt) -replace ‘(?s)(?\<=ID: Player.\*Atk: )\d{2,3}’,‘999’ | Set-Content c:\tast.txt

to this (input, the test.txt file):

```
{
    ID: NotPlayer,
    Lvl: 10,
    Atk: 30,
    Def: 50,
}
{
    ID: Player,
    Lvl: 10,
    Atk: 40,
    Def: 50,
}
```

and the result (tast.txt, new file created) is same with test.txt,

i am sorry for newbie question, but what did i wrong?

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [September 14, 2020, 6:05am UTC](https://forums.powershell.org/t/request-find-and-replace-using-powershell-regex/14941/6 "2020-09-14T06:05:50Z")

</div>

As I stated in my original reply, use -Raw on Get-Content. Let me know if that helps

---

<div class="post-metadata">

**Author:** ![kucingkucing](https://avatars.discourse-cdn.com/v4/letter/k/6a8cbe/32.png) [@kucingkucing](https://forums.powershell.org/u/kucingkucing)\
**Post date:** [September 14, 2020, 6:17am UTC](https://forums.powershell.org/t/request-find-and-replace-using-powershell-regex/14941/7 "2020-09-14T06:17:30Z")

</div>

thank you, now it works,

this the code :

(Get-Content c:\test.txt -raw) -replace ‘(?s)(?\<=ID: Player.\*Atk: )\d{2,3}’,‘999’ | Set-Content c:\tast.txt

now i just need to study what '(?s)(?\<= and \d{2,3} do,

thank you again!!!

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [September 14, 2020, 7:00am UTC](https://forums.powershell.org/t/request-find-and-replace-using-powershell-regex/14941/8 "2020-09-14T07:00:08Z")

</div>

Good deal. Hopefully this helps with your research.

**Single line mode**  
(?s) for “single line mode” makes the dot match all characters, including line breaks.  
This is why it required -Raw, making the entire text one line instead of multi line. It would’ve been much harder to achieve your desired result going line by line.

**Positive Lookbehind**  
(?\<=…)  
Ensures that the given pattern will match, ending at the current position in the expression. The pattern must have a fixed width. Does not consume any characters.  
This is what allowed it to only match when the specified text that came before.

\d matches any numeric character. {2,3} turned that into “match 2 or 3 numeric characters only”

---

<div class="post-metadata">

**Author:** ![kucingkucing](https://avatars.discourse-cdn.com/v4/letter/k/6a8cbe/32.png) [@kucingkucing](https://forums.powershell.org/u/kucingkucing)\
**Post date:** [September 14, 2020, 8:30am UTC](https://forums.powershell.org/t/request-find-and-replace-using-powershell-regex/14941/9 "2020-09-14T08:30:42Z")

</div>

thank you again for the reply.

for the (?s) and \d{2,3} is understandable, but for \<=… i google it, and found nothing about it,

do you have a link about it? I quite embarrass always asking the newbie questions

---

<div class="post-metadata">

**Author:** ![grokkit](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grokkit/32/970_2.png) [@grokkit](https://forums.powershell.org/u/grokkit)\
**Post date:** [September 14, 2020, 9:39am UTC](https://forums.powershell.org/t/request-find-and-replace-using-powershell-regex/14941/10 "2020-09-14T09:39:39Z")

</div>

Check out the RexEgg page about lookarounds: [Lookahead and Lookbehind Tutorial—Tips &Tricks](https://www.rexegg.com/regex-lookarounds.html)  
and the regular-expression.info page: [Regex Tutorial - Lookahead and Lookbehind Zero-Length Assertions](https://www.regular-expressions.info/lookaround.html)

---

<div class="post-metadata">

**Author:** ![kucingkucing](https://avatars.discourse-cdn.com/v4/letter/k/6a8cbe/32.png) [@kucingkucing](https://forums.powershell.org/u/kucingkucing)\
**Post date:** [September 14, 2020, 8:49pm UTC](https://forums.powershell.org/t/request-find-and-replace-using-powershell-regex/14941/11 "2020-09-14T20:49:23Z")

</div>

thank you grokkit, now i more (not fully, but more) understand about it

---

<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:28pm UTC](https://forums.powershell.org/t/request-find-and-replace-using-powershell-regex/14941/12 "2024-05-16T20:28:55Z")

</div>


