为什么此代码在PowerShell中工作,但它不';不要更改html文件中的任何内容(使用正则表达式)
我有为什么此代码在PowerShell中工作,但它不';不要更改html文件中的任何内容(使用正则表达式),powershell,powershell-4.0,Powershell,Powershell 4.0,我有file1.html,其中有以下几行: <bogus></bogus> <title>Something goes here</title> <TheEnd>END</TheEnd> 这里有些东西 结束 为了改变这一行,我用regex编写了3个不同的PowerShell脚本:这里有些东西: $path='c:\Folder1\file1.html' $Content=获取内容-路径$Path foreach($
file1.html
,其中有以下几行:
<bogus></bogus>
<title>Something goes here</title>
<TheEnd>END</TheEnd>
这里有些东西
结束
为了改变这一行,我用regex编写了3个不同的PowerShell脚本:这里有些东西
:
$path='c:\Folder1\file1.html'
$Content=获取内容-路径$Path
foreach($Content中的行){
$Line-replace“(.*?”,“$1 NEW now is that!”#此正则表达式选择标记之间的所有内容并进行替换:
}
设置内容-路径$Path-值$Line
及
$Content=Get Content-Path c:\Folder1\file1.html
foreach($Content中的行){
$Line-replace“(.*?”,“$1 NEW now is that!”#此正则表达式选择标记之间的所有内容并进行替换:
}
设置内容-路径$Path-值$Line
或
$path='c:\Folder1\file1.html'
$Content=获取内容-路径$Path
$GetTitle=[regex]“(.*)”
foreach($Content中的行){
$Line-replace$GetTitle,“$1 NEW现在就在那里!”#这个正则表达式选择标记之间的所有内容并进行替换:
}
设置内容-路径$Path-值$Line
输出应该是
现在有了新功能代码>
END
请注意,我的所有代码都在PowerShell中工作,但不会在File1.html
中进行任何更改。这就是问题所在。有人能纠正我的代码吗?使用regex-replace
,您需要考虑要保留什么,并在反向引用中捕获它。
在您的情况下,您希望保留
和
,并替换这些标记之间的内容
将正则表达式更改为'()*?()'
此外,您还可以使用Get Content上的-Raw
开关将文件作为单个多行字符串读取,进行替换并将结果直接传送到Set Content
$path = 'c:\Folder1\file1.html'
(Get-Content -Path $path -Raw) -replace '(<title>).*?(</title>)', '$1NEW is now there!$2' |
Set-Content -Path $Path
使用regex-replace
,您需要考虑要保留什么,并在反向引用中捕获它。
在您的情况下,您希望保留
和
,并替换这些标记之间的内容
将正则表达式更改为'()*?()'
此外,您还可以使用Get Content上的-Raw
开关将文件作为单个多行字符串读取,进行替换并将结果直接传送到Set Content
$path = 'c:\Folder1\file1.html'
(Get-Content -Path $path -Raw) -replace '(<title>).*?(</title>)', '$1NEW is now there!$2' |
Set-Content -Path $Path
$path='c:\Folder1\file1.html'
$Content=获取内容-路径$Path
$newContent=@()
$RegexForTitle='(?).*(=)'
foreach($Content中的行)
{
$newContent+=$Line-替换$RegexForTitle,'NEW现在就在这里!'
}
设置内容-路径$Path-值$newContent
#可选此行
“|输出文件-路径file1.html”
$path='c:\Folder1\file1.html'
$Content=获取内容-路径$Path
$newContent=@()
$RegexForTitle='(?).*(=)'
foreach($Content中的行)
{
$newContent+=$Line-替换$RegexForTitle,'NEW现在就在这里!'
}
设置内容-路径$Path-值$newContent
#可选此行
“|输出文件-路径file1.html”
您似乎没有向文件回写任何内容。您是否尝试了设置内容
之类的方法来保存更改?是的,我尝试了最后一行:设置内容-路径$Path-值$Content
您似乎没有将任何内容写回文件。您是否尝试了类似于设置内容
的方法来保存更改?是的,我尝试了最后一行:Set Content-Path$Path-Value$Content
$path = 'c:\Folder1\file1.html'
$Content = Get-Content -Path $path
$GetTitle = [regex]"<title>(.*?)</title>"
foreach($Line in $Content){
$Line -replace $GetTitle,'$1 NEW is now there!' #This regex selects everything between tags and make a replace:
}
Set-Content -Path $Path -Value $Line
$path = 'c:\Folder1\file1.html'
(Get-Content -Path $path -Raw) -replace '(<title>).*?(</title>)', '$1NEW is now there!$2' |
Set-Content -Path $Path
'$1' + Insert the text that was last matched by capturing group number 1
' NEW is now there!' + Insert the character string “ NEW is now there!” literally
'$2' Insert the text that was last matched by capturing group number 2
$path = 'c:\Folder1\file1.html'
$Content = Get-Content -Path $path
$newContent =@()
$RegexForTitle = '(?<=title>).*(?=</title>)'
foreach($Line in $Content)
{
$newContent += $Line -replace $RegexForTitle,'NEW IS NOW HERE!'
}
Set-Content -Path $Path -Value $newContent
#optional this line
'| Out-File -path file1.html'