代码之家  ›  专栏  ›  技术社区  ›  Eric Schoonover thSoft

PowerShell-匹配运算符和多个组

  •  13
  • Eric Schoonover thSoft  · 技术社区  · 16 年前

    我在PowerShell中处理以下日志条目,我试图使用 -match Regex

    相关PowerShell脚本

    $formattedMessage -match "(Get\sClient\sModel|Parse\sExpression|Get\sAbstract\sQuery|Compile\sQuery|Execute\sQuery|Get\sQuery\sPlan\sComplexity|Async\sTotal|Total)\s-\sduration\(([0-9]*)" | out-null
    $matches
    

    Name  Value
    ----  -----
    0     Get Client Model - duration(0
    1     Get Client Model
    2     0
    

    Timestamp: 11/9/2009 6:48:41 PM
    Message:
    Category: QueryService
    Priority: 3
    EventId: 1001
    Severity: Information
    Title: SPARQL Query Response
    Machine: SPOON16-SERVER
    App Domain: KnowledgeBaseHost.exe
    ProcessId: 2040
    Process Name: D:\QueryService\QSHost.exe
    Thread Name:
    Win32 ThreadId:8092
    Extended Properties:
    Key - Workflow_cbbdd58b-e574-4054-88d4-1dd7a56dc9d9
    Timeout - 1800
    Result Format - WireTable
    Result from Registry - False
    Compiled Query from Cache - True
    Result Count - 28332
    Query Plan Complexity - 661622
    Get Client Model - duration(0) start(0)
    Parse Expression - duration(0) start(0)
    Get Abstract Query - duration(0) start(0)
    Compile Query - duration(0) start(0)
    Get Query Plan - duration(0) start(1)
    Execute Query - duration(63695) start(1)
    Get Query Plan Complexity - duration(0) start(63696)
    Get Executed Operations - duration(0) start(63696)
    Total - duration(63696) start(0)
    Async Total - duration(63696) start(0)
    
    5 回复  |  直到 4 年前
        1
  •  11
  •   Peter Mortensen Pieter Jan Bonestroo    4 年前

    你可以用 Select-String -全部比赛

    $formattedMessage | Select-String 'regexpattern' -AllMatches
    

    -match operator您要做的主要事情是查找“a”匹配,即正则表达式模式是否匹配?

        2
  •  10
  •   Peter Mortensen Pieter Jan Bonestroo    4 年前

    我通过定义一个 Regex 然后打电话 PowerShell中的运算符。

    $detailRegex = [regex]"(Get\sClient\sModel|Parse\sExpression|Get\sAbstract\sQuery|Compile\sQuery|Execute\sQuery|Get\sQuery\sPlan\sComplexity|Async\sTotal|Total)\s-\sduration\(([0-9]*)"
    $detailRegex.Matches($formattedMessage)
    
        3
  •  4
  •   genio    16 年前

    http://www.johndcook.com/regex.html 举了一个很好的例子

    ^([^-]+)\s*-\s*duration\(([0-9]+)
    
    • 从行首开始
    • 捕捉第一个角色之前的所有角色-
    • 确保有-
    • 确保单词“duration()”存在
    • 捕获“持续时间(”之后的所有数字
        4
  •  4
  •   Peter Mortensen Pieter Jan Bonestroo    4 年前

    -match运算符只能使用一次;它不会对输入进行全局匹配。Keith Hill提出了一个建议 -火柴球 微软连接 here .

    我会建议另一种方法。如果日志条目在文件中,您可以使用switch语句来完成同样的事情:

    switch -regex -file .\log.txt { $entryRegex { $matches[1] + ", " + $matches[2] } }
    

    这是我通过以下语句得到的输出: $entryRegex 具有您定义的正则表达式:

    Get Client Model, 0
    Parse Expression, 0
    Get Abstract Query, 0
    Compile Query, 0
    Execute Query, 63695
    Get Query Plan Complexity, 0
    Total, 63696
    Async Total, 63696
    
        5
  •  0
  •   Nathan Hartley    14 年前

    Regular Expression Options 在一个表达式中,但遗憾的是,Global似乎不是可用的选项之一。

    推荐文章