PS脚本的正则表达式读取形式一行

时间:2016-04-07 20:12:12

标签: regex powershell

来自我希望提取特定内容的行的正则表达式。示例:在我的行中:这是测试2016/01/23这是测试04:05 AM这是测试成功登录01.33秒。我怎样才能只提取日期,时间和成功时间。所以输出将是

(2016/01/23 04:05 AM成功01.33秒)。

感谢您的帮助。干杯!

1 个答案:

答案 0 :(得分:0)

我怀疑我会回答,然后你会发表评论"我的真实数据完全不同我如何匹配?" 。我们来看看。

# Here is your log line
$logLine = 'This is a test 2016/01/23 This is test 04:05 AM this is a test  Success Login at 01.33 sec'

# Here is my regex which matches
#           *    date          *    time    AMPM * Success Login      time       *
$myRegex = '.*(\d{4}/\d\d/\d\d).*(\d\d:\d\d \S\S).*Success Login at ([0-9.]+ \S+).*'

# Here is the test and output building
if ($logLine -match $myRegex) { 
    Write-Host "($($matches[1]) $($matches[2]) Success $($matches[3]))"
} else {
    Write-Host "Log doesn't match"
}