替换文件中 html 文本的小块
Replacing small blocks of html text in a file
我一直在寻找答案很多天,但我无法解决。
在我的例子中,我需要替换 .htm 文件中的字符串。字符串跨多行,例如:
</table>
<p>
<table border="1">
我尝试了以下方法,但它不起作用,我知道是因为文本出现在多行中。
Get-Content test1.htm )| ForEach{ $_ -replace '</table><p><table border="1">', "" } | Set-Content test2.htm
我不知道我该怎么做。
试试这个:
(Get-Content 'filename').replace('toreplace', 'replacewith') | Set-Content 'filename'
好的,有人设法解决了,所以我post回答了。线索是包括 Poweshell 溢出运算符:\s+.
所以我的最终代码如下:
$filenames = @("test1.htm")foreach ($file in $filenames) {
$replacementStr = ''
(Get-Content $file | Out-String) |
Foreach-object { $_ -replace '</table>.\s+<p>\s+<table border="1">' , $replacementStr } |
Set-Content test2.htm }
我一直在寻找答案很多天,但我无法解决。 在我的例子中,我需要替换 .htm 文件中的字符串。字符串跨多行,例如:
</table>
<p>
<table border="1">
我尝试了以下方法,但它不起作用,我知道是因为文本出现在多行中。
Get-Content test1.htm )| ForEach{ $_ -replace '</table><p><table border="1">', "" } | Set-Content test2.htm
我不知道我该怎么做。
试试这个:
(Get-Content 'filename').replace('toreplace', 'replacewith') | Set-Content 'filename'
好的,有人设法解决了,所以我post回答了。线索是包括 Poweshell 溢出运算符:\s+.
所以我的最终代码如下:
$filenames = @("test1.htm")foreach ($file in $filenames) {
$replacementStr = ''
(Get-Content $file | Out-String) |
Foreach-object { $_ -replace '</table>.\s+<p>\s+<table border="1">' , $replacementStr } |
Set-Content test2.htm }