0

I have a test.lua (UTF-8) file:

function private.PostAuctionsThread(self, auctionInfo)
self:SetThreadName("SHOPPING_POST_AUCTIONS")
local auctionRecord = auctionInfo.record
local postFrame = private.frame.content.result.confirmation.post
private.frame.content.result.cancelBtn:Disable()
private.frame.content.result.postBtn:Disable()
private.frame.content.result.bidBtn:Disable()
private.frame.content.result.buyoutBtn:Disable()
postFrame.postBtn:Enable()
postFrame.closeBtn:Enable()

local undercut = (TSMAPI:GetCustomPriceValue(TSM.db.global.postUndercut, auctionRecord.itemString) or 1)
if undercut > auctionRecord.itemBuyout then
    undercut = 1
end
if TSMAPI.Player:IsPlayer(auctionRecord.seller, true, true, true) then
    -- don't undercut ourselves
    undercut = 0
end
local auctionBuyout
if auctionRecord.isFake then
    auctionBuyout = TSMAPI:GetCustomPriceValue(TSM.db.global.normalPostPrice, auctionRecord.itemString) or 1000000
else
    auctionBuyout = auctionRecord.buyout - (undercut * auctionRecord.stackSize)
end

and I want to remove/replace this pattern

<tab here>if TSMAPI.Player:IsPlayer(auctionRecord.seller, true, true, true) then
    <tab><tab>-- don't undercut ourselves
    <tab><tab>undercut = 0
<tab here>end

via Powershell Script:

$text ="pattern_here!"
$content = [System.IO.File]::ReadAllText(path_to\test.lua")
(Get-Content 'path_to\test.lua')  -replace $test,"example" | Set-Content 'path_to\test.lua'

So the script does replace/remove operations perfectly , but only in one string.

What I have already tried: use $text.contains with

`r `n `t `s — Escape characters

and even if PowerShell reply is "true" is doesn't change anything

this script from StackOverflow but it changes every string with "end", not only one which following next to "undercut = 0"

So, what am I join wrong? Already waste 3 H+ on this. Is it possible to use mask where first string should be start of $pattern and the first "end" will be the closing one?

Or I should covert all this strings to array? Or...use concat? Any ideas?

Claire Furney
  • 2,115
  • 3
  • 24
  • 36
AlexZeDim
  • 3,520
  • 2
  • 28
  • 64

2 Answers2

1

One option:

$text = 
@'
   if TSMAPI.Player:IsPlayer(auctionRecord.seller, true, true, true) then
            -- don't undercut ourselves
            undercut = 0
    end
Keep this text
   if TSMAPI.Player:IsPlayer(auctionRecord.seller, true, true, true) then
            -- don't undercut ourselves
            undercut = 0
    end

'@

$replace_text = 
@'
   if TSMAPI.Player:IsPlayer(auctionRecord.seller, true, true, true) then
            -- don't undercut ourselves
            undercut = 0
    end
'@

$regex = "(?ms)$([regex]::Escape($replace_text))"

$text -replace $regex,"example"

The regex is built with [regex]::Escape(), using text that's simply copied and pasted from a sample of the data. The (?ms) option makes it work with multi-line data

mjolinor
  • 66,130
  • 7
  • 114
  • 135
  • Thank you, I modify this one a bit, but it's doesn't work. Actually I already trying to find an answer for this problem via different forums. So let's skip the introduction and going to the "fun" part: $regex = "(?ms)$([regex]::Escape($replace_text))" $text -replace $regex,"example" | Set-Content 'path_to/test.lua' I'm going to try this with masks: \tif tsmapi(.|\r|\n|\t)*? - \tif tsmapi(.|\r|\n|\t)*?end Original lua can be found here: [Github](https://github.com/AlexZeDim/BlackOps_Model/blob/master/AuctionTab.lua) – AlexZeDim Feb 18 '17 at 23:05
0

Okay it's a bit unusual but I waste almost 4 and 1/2 hour for this. For all those who will read this, use (Get-Content 'path_to\test.file' -raw) if you are working with something different from usual .txt file, not matter "filetype" or "charset".

AlexZeDim
  • 3,520
  • 2
  • 28
  • 64