string - 在Powershell中从字符串中提取字符串

标签 string powershell teamcity

我有一个字符串 long string: its a teamcity buildLog这是来自 teamcity 的 buildLog。

[11:27:30] :     [Step 5/5] 15:27:30 INFO: Average times: total 0.455, latency 0.455, connect 0.004
[11:27:30] :     [Step 5/5] 15:27:30 INFO: Percentiles:
[11:27:30] :     [Step 5/5] +---------------+---------------+
[11:27:30] :     [Step 5/5] | Percentile, % | Resp. Time, s |
[11:27:30] :     [Step 5/5] +---------------+---------------+
[11:27:30] :     [Step 5/5] |           0.0 |         0.021 |
[11:27:30] :     [Step 5/5] |          50.0 |         0.103 |
[11:27:30] :     [Step 5/5] |          90.0 |         1.166 |
[11:27:30] :     [Step 5/5] |          95.0 |          2.27 |
[11:27:30] :     [Step 5/5] |          99.0 |          2.77 |
[11:27:30] :     [Step 5/5] |          99.9 |         6.996 |
[11:27:30] :     [Step 5/5] |         100.0 |        10.312 |
[11:27:30] :     [Step 5/5] +---------------+---------------+
[11:27:30] :     [Step 5/5] 15:27:30 INFO: Request label stats:
[11:27:30] :     [Step 5/5] +------------------------------------------+--------+---------+--------+-------------+
[11:27:30] :     [Step 5/5] | label                                    | status |    succ | avg_rt | error       |
[11:27:30] :     [Step 5/5] +------------------------------------------+--------+---------+--------+-------------+
[11:27:30] :     [Step 5/5] | Activity History                         |   OK   | 100.00% |  1.608 |             |
[11:27:30] :     [Step 5/5] | Asset Allocation                         |   OK   | 100.00% |  0.100 |             |
[11:27:30] :     [Step 5/5] | Dashboard Cards and Employee Information |   OK   | 100.00% |  0.255 |             |
[11:27:30] :     [Step 5/5] | Fund Details                             |   OK   | 100.00% |  0.825 |             |
[11:27:30] :     [Step 5/5] | Investments                              |   OK   | 100.00% |  0.132 |             |
[11:27:30] :     [Step 5/5] | Minimum Version                          |   OK   | 100.00% |  0.032 |             |
[11:27:30] :     [Step 5/5] | Rate of Return                           |   OK   | 100.00% |  0.047 |             |
[11:27:30] :     [Step 5/5] | Retirement Outlook Card                  |   OK   | 100.00% |  1.166 |             |
[11:27:30] :     [Step 5/5] | Retirement Outlook Full                  |   OK   | 100.00% |  1.160 |             |
[11:27:30] :     [Step 5/5] | Savings Rate                             |   OK   | 100.00% |  0.112 |             |
[11:27:30] :     [Step 5/5] | Secure Auth Login                        |  FAIL  |  98.58% |  0.207 | Bad Request |
[11:27:30] :     [Step 5/5] | Validate Savings Rate Change             |   OK   | 100.00% |  0.127 |             |
[11:27:30] :     [Step 5/5] | Vested Balance                           |   OK   | 100.00% |  0.157 |             |
[11:27:30] :     [Step 5/5] +------------------------------------------+--------+---------+--------+-------------+
[11:27:35] :     [Step 5/5] 15:27:35 INFO: Ending data feeding...
[11:27:36] :     [Step 5/5] 15:27:36 INFO: Online report link: https://a.blazemeter.com/app/#/masters/36669958
从上面的构建日志中,我必须获取 Percentiles Table , Request Label Stats表和Online Report Link我尝试了下面的代码,但它没有返回:
$firststring = "Percentiles:"
$secondstring = "Request label stats:"
$pattern =  "$firststring(.*?)$secondstring"
$result = [regex]::Match($file,$pattern).Groups[1].Value
$result >> returns none
和下面的代码来获取字符串。
$Regex = [Regex]::new("(?<=Percentiles:)(.*)(?=Request label stats:)")
$Match = $Regex.Match($status)
if($Match.Success)
{
  $Match.Value
}
这也没有返回。
任何帮助将非常感激。

最佳答案

我无法为您检查 PowerShell 3.0。但以下内容适用于 Windows PowerShell 5.1。我有两种解决方案,一种包括作为比赛一部分的第一个信息行,另一种不包括。

# Set up your strings to look for
$firststring = [regex]::Escape("Percentiles:")
$secondstring = [regex]::Escape("Request label stats:")

# Pattern which includes the line with $firststring
$withInfoPattern = ".*$firststring(.*\n)*(?=.*$secondstring)"

# Pattern which omits the line with $firststring
$withoutInfoPattern = "(?<=$firststring\s+)(.*\n)*(?=.*$secondstring)"

# We will use $withInfoPattern in this example but you could also use $withoutInfoPattern
# This assumes your content string is in a variable called $content
$matchedContent = if( $content -match $withInfoPattern ) {
  $matches[0]
}

This shows how to match on your first case, the Percentiles table data. To grab the second table and report link you should be able to use the code above and explanations below to extract this data as well as a learning exercise.


此时$matchedContent将包含您正在寻找的输出。您也可以使用 $withoutInfoPattern 进行匹配相反,如果您不希望返回匹配的第一行。我将在下面解释这一点:
  • 虽然在这种情况下不需要,但最好通过 [regex]::Escape(string) 将要插入到正则表达式模式中的字符串放入。 .这是一个很好的做法,可以防止您将来无意中忘记使用特殊的正则表达式字符转义字符串。
  • $withInfoPattern匹配任何前面带或不带字符的行 $firststring . $withoutInfoPattern而是使用正向后视来确保匹配的内容发生 $firstString .
  • (.*\n)*使用捕获组匹配任何字符或不匹配字符(这就是 .* 的意思),然后是换行符。换行符默认不匹配 .并且无法通过 -match 更改此行为运算符(operator)。尾随 *查找前面捕获组的任何实例或不查找实例。
  • -match通常不会跨换行匹配。使用 (.*\n)*这样就绕过了这个限制。

  • (?=.*$secondString)是正向前瞻,以确保模式主模式在前瞻中指定的模式之前。 .*$secondString将从该行的开头开始匹配,因此我们正在寻找后跟 $secondString 的任何字符在那条线上。
  • 调用 -match运营商寻找$withInfoPattern (或 $withoutInfoPattern)在 $content 内.这假设您要搜索的字符串存储在名为 $content 的变量中。作为字符串(不是字符串数组,因为 Get-Content 将默认执行。
  • 您可以使用 $content = Get-Content -Raw将文件作为带有换行符的单个字符串读取或加入 $content在匹配之前具有换行符的数组:$content -join "`n"

  • -match将返回 $true$false .如 $true ,可以从自动变量$matches中获取匹配的内容在第一个索引中。由于数组使用从零开始的索引,这转化为 $matches[0] .

  • 其他正则表达式提示
    我强烈建议您使用该网站 https://regexr.com/来测试你的表达式。虽然它只支持 JavaScript 和 PCRE 引擎,但两者都足够接近 .NET regex 引擎,所以它通常不是问题。即使您发现某些表达式不起作用,它也很好地解释了不同的正则表达式标记,并且在您开发表达式时,它会在底部 Pane 中解释每个字符序列的含义。

    关于string - 在Powershell中从字符串中提取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69586790/

    相关文章:

    python - 如何使用正则表达式判断字符串的第一个和最后一个字符是否匹配?

    .net - Powershell:列出 .net 组件的成员

    powershell - 如何避免将未知值添加到数组列表中?

    msbuild - 使用 TeamCity 构建和部署特定的修订号

    Javascript - 如何编写递归函数来构建像自动换行这样的字符串数组?

    java - 确保单词具有特定长度

    powershell - 如何在用户 session 中运行Powershell ScheduledJob(-AtLogOn)?

    svn - 如何清除TeamCity 7+中未决的VCS更改?

    github - TeamCity 代理错误 "failed to perform checkout on agent"

    java - 根据数字/字符模式拆分字符串 - Java