PowerShell -match operator and multiple groups

前端 未结 5 1626
南笙
南笙 2021-02-20 01:28

I have the following log entry that I am processing in PowerShell I\'m trying to extract all the activity names and durations using the -match operator but I am onl

5条回答
  •  长情又很酷
    2021-02-20 01:49

    The -match operator is meant to be used just once; it doesn't do a global match on the input. Keith Hill put a suggestion for a -matchall operator on Microsoft connect here.

    I'll suggest another way to do it. If the log entry is in a file, you can use the switch statement to accomplish the same thing:

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

    This is the output I get with this statement if $entryRegex has the regular expression you defined:

    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
    

提交回复
热议问题