相关疑难解决方法(0)

如何在Powershell中使用正则表达式选择“捕获”代码块?

我正在尝试分析许多目录中的许多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类型的正则表达式。

谢谢。

regex powershell parsing

3
推荐指数
1
解决办法
38
查看次数

标签 统计

parsing ×1

powershell ×1

regex ×1