REGEX powershell脚本删除调试标记

REGEX powershell脚本删除调试标记,regex,powershell,Regex,Powershell,我有一个powershell scrip测试。ps1: #if DEBUG Write-Output "LOG 1" #endif Write-Output "LOG 2" #if DEBUG Write-Output "LOG 3" #endif Write-Output "LOG 4" 我想要一个删除调试标记的方法,它们之间有代码。 我会得到这个: Write-Output "LOG 2" Write-Output "LOG 4" 我设法说: $src = "C:\test.ps1" $

我有一个powershell scrip测试。ps1:

#if DEBUG
Write-Output "LOG 1"
#endif
Write-Output "LOG 2"
#if DEBUG
Write-Output "LOG 3"
#endif
Write-Output "LOG 4"
我想要一个删除调试标记的方法,它们之间有代码。 我会得到这个:

Write-Output "LOG 2"
Write-Output "LOG 4"
我设法说:

$src = "C:\test.ps1"
$out = "C:\test2.ps1"
(Get-Content $src -Raw) -creplace "(?m)^(#if DEBUG)(?s).+(?m)^(#endif)","" | Set-Content -Encoding Default $out
但是输出,我只得到以下结果:

Write-Output "LOG 4"
我认为这是因为该命令删除了第一个标记if和最后一个标记endif之间的所有内容。不考虑其他标签之间


你能帮我吗?

像下面这样更改你的正则表达式

-creplace "(?m)^(?:#if DEBUG)[\s\S]+?\n#endif[\r\n]*",""
如有必要,请再次退出反斜杠


为什么不直接使用预处理器?!用写调试替换写输出,然后删除,如果调试完全是简单的搜索和替换。将脚本设置为高级函数,然后使用-Debug开关获取调试输出。