考虑以下通用正则表达式的 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,})?)
返回出现在“时间:”之后和时间和“[ms]”之间的空格之前的所有小数,有效数字最多只能有一个小数点
- 最后的逻辑解析所有找到的匹配值
- 当在 powershell 中使用正则表达式进行匹配时,会自动填充 $matches 数组