【问题标题】:Regex pattern for keyword with ( bracket带有 ( 括号的关键字的正则表达式模式
【发布时间】:2018-08-15 17:51:30
【问题描述】:

需要使用 PowerShell 在文档上搜索以下关键字:

$keyword = "The Parent shall pay to the Security Agent ("
Get-Content $SourceFileName | Select-String -Pattern $keyword

$keyword 有 "(" 左括号 - 如何在 powershell 中使用正则表达式提及这一点

【问题讨论】:

    标签: powershell


    【解决方案1】:

    默认情况下Select-String 进行正则表达式匹配。在正则表达式中,括号具有特殊含义:它们将子表达式和断言组合在一起。为了匹配正则表达式中的文字括号,必须使用反斜杠对其进行转义。

    一般来说,如果您希望 Select-String 将模式匹配为文字字符串,请使用 -SimpleMatch 参数:

    Get-Content $SourceFileName | Select-String -Pattern $keyword -SimpleMatch
    

    或转义模式:

    Get-Content $SourceFileName | Select-String -Pattern ([regex]::Escape($keyword))
    

    【讨论】:

      【解决方案2】:

      用反斜杠\转义左括号

      $keyword = "The Parent shall pay to the Security Agent \("
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-07
        • 2014-07-01
        相关资源
        最近更新 更多