request : find and replace using PowerShell (regex)

Welcome Forums General PowerShell Q&A request : find and replace using PowerShell (regex)

Viewing 10 reply threads
  • Author
    Posts
    • #256328
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      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:

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

      the code i want is find:

      then replace it with:

      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.

    • #256337
      Participant
      Topics: 8
      Replies: 568
      Points: 2,171
      Helping Hand
      Rank: Community Hero

      Hopefully this example will help you.

      Output

      Here is how it would look reading from a file.

    • #256364
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      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

       

    • #256433
      Participant
      Topics: 8
      Replies: 568
      Points: 2,171
      Helping Hand
      Rank: Community Hero

      OK, perhaps this example will help more.

      Output

    • #256454
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      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):

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

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

    • #256469
      Participant
      Topics: 8
      Replies: 568
      Points: 2,171
      Helping Hand
      Rank: Community Hero

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

    • #256481
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      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!!!

    • #256496
      Participant
      Topics: 8
      Replies: 568
      Points: 2,171
      Helping Hand
      Rank: Community Hero

      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”

    • #256514
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

      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

    • #256532
      Senior Moderator
      Topics: 10
      Replies: 168
      Points: 899
      Helping Hand
      Rank: Major Contributor

      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

    • #256637
      Participant
      Topics: 1
      Replies: 5
      Points: 22
      Rank: Member

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

Viewing 10 reply threads
  • You must be logged in to reply to this topic.