Regex 查找跨文本的多行并使用PowerShell替换

Regex 查找跨文本的多行并使用PowerShell替换,regex,powershell,Regex,Powershell,我使用正则表达式搜索来匹配和替换一些文本。文本可以跨多行(可能有也可能没有换行符)。 目前我有: $regex = "\<\?php eval.*?\>" Get-ChildItem -exclude *.bak | Where-Object {$_.Attributes -ne "Directory"} |ForEach-Object { $text = [string]::Join("`n", (Get-Content $_)) $text -replace $RegEx

我使用正则表达式搜索来匹配和替换一些文本。文本可以跨多行(可能有也可能没有换行符)。 目前我有:

 $regex = "\<\?php eval.*?\>"

Get-ChildItem -exclude *.bak | Where-Object {$_.Attributes -ne "Directory"} |ForEach-Object {
 $text = [string]::Join("`n", (Get-Content $_))
 $text -replace $RegEx ,"REPLACED"}
$regex=“\”
Get-ChildItem-exclude*.bak | Where对象{$\属性-ne“Directory”}ForEach对象{
$text=[string]::Join(`n',(获取内容$)
$text-替换$RegEx,“替换”}
试试这个:

$regex = New-Object Text.RegularExpressions.Regex "\<\?php eval.*?\>", ('singleline', 'multiline')

Get-ChildItem -exclude *.bak |
  Where-Object {!$_.PsIsContainer} |
  ForEach-Object {
     $text = (Get-Content $_.FullName) -join "`n"
     $regex.Replace($text, "REPLACED")
  }
$regex=新对象Text.RegularExpressions.regex“\”,(“单线”、“多线”)
获取子项-排除*.bak|
Where对象{!$\ PsIsContainer}|
ForEach对象{
$text=(获取内容$\ux.FullName)-加入“`n”
$regex.Replace($text,“REPLACED”)
}

通过显式创建正则表达式,以便传入选项。

尝试将正则表达式模式更改为:

 "(?s)\<\?php eval.*?\>"
“(?s)\”
获取单线(点匹配任何字符,包括行终止符)。由于您没有使用
^
$
元字符,我认为您不需要指定多行(
^
&
$
匹配嵌入式行终止符)

更新:似乎-replace确保正则表达式不区分大小写,因此不需要
i
选项。

应该使用
(.|\n)+
表达式来跨越行边界
因为
与新行不匹配。

我认为您需要
Where Object{!$\uuu.PSIsContainer}
,而且这绝对是一种更好的方法(相对于测试属性)。