0

以下代码有效,但它替换了它找到的所有文本。有没有办法只替换第一次出现。我已经尝试了 4 天。

请帮忙。

cls   
$fileName = "c:\test\bmsbackuplog.htm"  
$findstring=Select-string $fileName -Pattern "The backup of volume system reserved" -list 
$replacement = "<tr><td>Change using this line</td></tr>"    
$firstdata=$findstring[0].Line 
$both="$firstdata$replacement"  
$match ="The backup of volume system reserved*.*"  
(Get-Content ($fileName)) -replace $match,$both | Set-Content "c:\test\bmsbackuplog.htm"
4

1 回答 1

0

我会为此使用零宽度负后向断言:

$fileName = "c:\test\bmsbackuplog.htm"
$pattern = 'The backup of volume system reserved'
$replacement = '$1<tr><td>Change using this line</td></tr>' 
[io.file]::ReadAllText($fileName) -replace "(?<!$pattern[\s\S]*)($pattern)", $replacement
于 2013-04-18T18:11:00.647 回答