Tom*_*lly 3 regex powershell parsing
我正在尝试分析许多目录中的许多Powershell脚本,并且我想将任何Catch代码块拉到列表/变量中。
我正在尝试编写正则表达式以选择以下格式的任何块
Catch
{
write-Host "Function:",$MyInvocation.MyCommand,"Failed with exception:" "Error"
write-Host "Exception: $_" "Error"
throw "Exception: $_"
}
Run Code Online (Sandbox Code Playgroud)
Catch{
write-Host "Function:",$MyInvocation.MyCommand,"Failed with exception:" "Error"
write-Host "Exception: $_" "Error"
throw "Exception: $_" }
Run Code Online (Sandbox Code Playgroud)
Catch {write-Host "Function:",$MyInvocation.MyCommand,"Failed with exception:" "Error"
write-Host "Exception: $_" "Error"
throw "Exception: $_"}
Run Code Online (Sandbox Code Playgroud)
基本上在任何地方都有一个接在{}之后的catch,忽略单词“ Catch”和花括号之间以及花括号之后的换行符,忽略大小写。
我也希望返回{}之间的全部内容,以便我可以对其进行一些其他检查。
我设法弄出的最好的是:
\b(\w*Catch\w*)\b.*\w*{\w.*}
Run Code Online (Sandbox Code Playgroud)
如果全部在一条线上,则将匹配。
我将在powershell中进行此操作,因此将非常感谢.net或powershell类型的正则表达式。
谢谢。
请改用PowerShell分析器!
foreach($file in Get-ChildItem *.ps1){
$ScriptAST = [System.Management.Automation.Language.Parser]::ParseFile($file.FullName, [ref]$null, [ref]$null)
$AllCatchBlocks = $ScriptAST.FindAll({param($Ast) $Ast -is [System.Management.Automation.Language.CatchClauseAst]}, $true)
foreach($catch in $AllCatchBlocks){
# The catch body that you're trying to capture
$catch.Body.Extent.Text
# The "Extent" property also holds metadata like the line number and caret index
$catch.Body.Extent.StartLineNumber
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
38 次 |
最近记录: |