请考虑下面的通用正则表达式的powershell示例。
Time:\s*((?<![.])[0-9]*?([.][0-9]{1,})?)\s
例
$Matches = @()
$String = 'Time: 3.610 [ms] (mean)
Time: 1.805 [ms] (mean, across all concurrent requests)'
Write-Host start with
write-host $String
Write-Host
Write-Host found
([regex]'Time:\s*((?<![.])[0-9]*?([.][0-9]{1,})?)\s').matches($String) | foreach {
write-host "key at $($_.Groups[1].Index) = '$($_.Groups[1].Value)'"
} # next match
息率
start with
Time: 3.610 [ms] (mean)
Time: 1.805 [ms] (mean, across all concurrent requests)
found
key at 12 = '3.610'
key at 43 = '1.805'
摘要
((?<![.])[0-9]*?([.][0-9]{1,})?)
返回之后出现所有十进制数 “时间:” 和日之前e时间和“[ms]”之间的空格,有效的数字必须至多有一次小数点
- 最后的逻辑解析所有匹配的找到的值
- $ matches使用正则表达式时自动填充数组在PowerShell中
匹配
你的代码打印既3.610 1.805和我。 – toolic 2013-05-01 19:23:24
我认为我们需要看到更多的代码。我得到两个时间值。(关闭while循环后) – Nate 2013-05-01 19:23:26
它可能会打印两个值,但问题是如何仅提取第一个值 – Dcoder 2013-05-01 19:30:10