Welcome › Forums › General PowerShell Q&A › request : find and replace using PowerShell (regex)
- This topic has 10 replies, 3 voices, and was last updated 4 months, 2 weeks ago by
Participant.
-
AuthorPosts
-
-
September 14, 2020 at 1:27 am #256328
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:
XHTML123456{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:
XHTML123ID: Player,Lvl: /*ignore this text*/,Atk: *,then replace it with:
PowerShell123ID: 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.
-
September 14, 2020 at 3:21 am #256337
Hopefully this example will help you.
PowerShell12345678910$text = @'{ID: Player,Lvl: 10,Atk: 30,Def: 50,}'@$text -replace '(?<=Atk: )\d{2,3}','999'Output
PowerShell123456{ID: Player,Lvl: 10,Atk: 999,Def: 50,}Here is how it would look reading from a file.
PowerShell123$text = get-content c:\path\to\jsfile.js -raw$text -replace '(?<=Atk: )\d{2,3}','999' -
September 14, 2020 at 5:17 am #256364
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
-
September 14, 2020 at 10:32 am #256433
OK, perhaps this example will help more.
PowerShell12345678910111213141516$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
PowerShell123456789101112{ID: NotPlayer,Lvl: 10,Atk: 30,Def: 50,}{ID: Player,Lvl: 10,Atk: 999,Def: 50,} -
September 14, 2020 at 11:15 am #256454
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):
JavaScript123456789101112{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?
-
September 14, 2020 at 11:35 am #256469
As I stated in my original reply, use -Raw on Get-Content. Let me know if that helps
-
September 14, 2020 at 11:47 am #256481
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!!!
-
September 14, 2020 at 12:30 pm #256496
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”
-
September 14, 2020 at 2:00 pm #256514
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
-
September 14, 2020 at 3:09 pm #256532
Check out the RexEgg page about lookarounds: https://www.rexegg.com/regex-lookarounds.html
and the regular-expression.info page: https://www.regular-expressions.info/lookaround.html -
September 15, 2020 at 2:19 am #256637
thank you grokkit, now i more (not fully, but more) understand about it
-
-
AuthorPosts
- The topic ‘request : find and replace using PowerShell (regex)’ is closed to new replies.