我想从Windows Powershell脚本中使用REGEX的详细描述中了解运行进程的状态。
我想从此字符串中提取RUNNING

最佳答案

要提取RUNNINGSTOPPED等,您可以尝试以下操作:

PS > $s = "Name: Process_name Started 2008-04-21 11:33 Status RUNNING Checkpoint Lag 00:00:00", "Name: Process2_name Started 2008-04-21 11:33 Status STOPPED Checkpoint Lag 00:00:00"

PS > $s | % { if ($_ -match "Status (.+) Checkpoint") {
    #Return match from group 1
    $Matches[1]
    }
}

RUNNING
STOPPED

如果您正在读取日志文件,则可以将其内容直接发送给测试,如下所示:
PS > Get-Content mylog.txt | % { if ($_ -match "Status (.+) Checkpoint") {
    #Return match from group 1
    $Matches[1]
    }
}

只要提取至少1个字符(也可以是多个单词),这将提取“状态”和“检查点”之间的所有内容。

关于powershell - 想要在Powershell脚本中使用正则表达式从字符串中提取子字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14751915/

10-11 07:48
查看更多