我有一個字串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
}
這也沒有回傳。任何幫助將非常感激。
uj5u.com熱心網友回復:
我無法為您檢查 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]
}
這顯示了如何匹配您的第一個案例Percentiles表資料。要獲取第二個表格和報告鏈接,您應該能夠使用上面的代碼和下面的解釋來提取這些資料以及學習練習。
此時$matchedContent將包含您正在尋找的輸出。$withoutInfoPattern如果您不希望回傳匹配的第一行,您也可以匹配 using 。我將在下面解釋這一點:
- 雖然在這種情況下不是必需的,但最好將將通過
[regex]::Escape(string). 這是一個很好的做法,可以防止您將來無意中忘記使用特殊的正則運算式字符轉義字串。 $withInfoPattern匹配任何前面有或沒有字符的行$firststring。$withoutInfoPattern而是使用正向后視來確保匹配的內容發生$firstString.(.*\n)*使用捕獲組匹配任何字符或不匹配字符(這就是什么.*意思),然后是換行符。默認情況下,換行符不與 with 匹配,.并且無法使用-match運算子更改此行為。尾隨*查找前面捕獲組的任何實體或不查找任何實體。-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 正則運算式引擎,因此通常不會出現問題。即使您發現某些運算式不起作用,它也很好地解釋了不同的正則運算式標記,并且在您開發運算式時,它會在底部窗格中解釋每個字符序列的含義。
uj5u.com熱心網友回復:
一旦你有了正則運算式模式,你就可以switch用來運行日志行并從你的表中構建物件。下面我將其分解為單獨的功能。
$log = @'
[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
'@ -split '\r?\n'
function get-percentiles {
param()
$headerPattern = '\| Percentile'
$endPattern = '^[^| ]*$'
$inTable = $false
switch -regex ($log) {
$headerPattern {
$inTable = $true
continue
}
$endPattern {
if ($inTable) { break } else { continue }
}
'\|([^|] )\|([^|] ).*$' {
if ($inTable) {
[PSCustomObject]@{
Percentile = $Matches[1].Trim()
Time = $Matches[2].Trim()
}
}
}
}
}
function get-labeltable {
param()
$headerPattern = '\| label'
$endPattern = '^[^| ]*$'
$inTable = $false
switch -regex ($log) {
$headerPattern {
$inTable = $true
continue
}
$endPattern {
if ($inTable) { break } else { continue }
}
'\|([^|] )\|([^|] )\|([^|] )\|([^|] )\|([^|] ).*$' {
if ($inTable) {
[PSCustomObject]@{
Label = $Matches[1].Trim()
Status = $Matches[2].Trim()
Succ = $Matches[3].Trim()
AvgRT = $Matches[4].Trim()
Error = $Matches[5].Trim()
}
}
}
}
}
get-percentiles | Format-Table
get-labeltable | Format-Table
輸出
Percentile Time
---------- ----
0.0 0.021
50.0 0.103
90.0 1.166
95.0 2.27
99.0 2.77
99.9 6.996
100.0 10.312
Label Status Succ AvgRT Error
----- ------ ---- ----- -----
Activity History OK 100.00% 1.608
Asset Allocation OK 100.00% 0.100
Dashboard Cards and Employee Information OK 100.00% 0.255
Fund Details OK 100.00% 0.825
Investments OK 100.00% 0.132
Minimum Version OK 100.00% 0.032
Rate of Return OK 100.00% 0.047
Retirement Outlook Card OK 100.00% 1.166
Retirement Outlook Full OK 100.00% 1.160
Savings Rate OK 100.00% 0.112
Secure Auth Login FAIL 98.58% 0.207 Bad Request
Validate Savings Rate Change OK 100.00% 0.127
Vested Balance OK 100.00% 0.157
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/317668.html
上一篇:從字串中洗掉串列或陣列的元素
下一篇:將分隔的字串/文本轉換為地圖物件
