![](/img/trans.png)
[英]Matching characters (inc. newlines) in a regex until next match is found
[英]PCRE Regex Matching Patterns until next pattern is found
我正在努力尋找這種正則表達式的解決方案,這似乎很簡單。 我需要匹配另一個匹配模式之前的模式。
我需要捕獲以下“ Keberos-wsfed”后面的“ Mean:”:
Kerberos:
Historical:
Between 26 and 50 milliseconds: 10262
Between 50 and 100 milliseconds: 658
Between 101 and 200 milliseconds: 9406
Between 201 and 500 milliseconds: 6046
Between 501 milliseconds and 1 second: 1646
Between 1 and 5 seconds: 1399
Between 6 and 10 seconds: 13
Between 11 and 30 seconds: 34
Between 31 seconds and 1 minute: 7
Between 1 minute and 2 minutes: 1
Mean: 268, Mode: 36, Median: 123
Total: 29472
Kerberos-wsfed:
Historical:
Between 26 and 50 milliseconds: 3151
Between 50 and 100 milliseconds: 129
Between 101 and 200 milliseconds: 650
Between 201 and 500 milliseconds: 411
Between 501 milliseconds and 1 second: 171
Between 1 and 5 seconds: 119
Between 6 and 10 seconds: 4
Between 11 and 30 seconds: 6
Between 1 minute and 2 minutes: 1
Mean: 176, Mode: 33, Median: 37
Total: 4642
我可以匹配(?:Kerberos-wsfed :),我可以匹配Mean:但是我必須在Kerberos-wsfed之后找到平均值,但有困難。 感謝您的協助。
使用捕獲組:
Kerberos-wsfed:[\s\S]*Mean:\s(\d+)
Kerberos-wsfed:
匹配文字 [\\s\\S]*
允許之間使用任意數量的字符(包括行定界符) Mean:\\s
匹配文字Mean
后跟空格\\s
(\\d+)
捕獲您要查找的值。 它實際上允許任何數量的數字 您要查找的值( 176
)將位於第一個捕獲組中,該組為$1
或基於您的語言的第一個捕獲組。 例如,在PHP中:
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
echo $matches[0][1];
// Output: 176
嘗試使用該常規#Kerberos-wsfed:.+?Mean:\\s+(\\d+)#s
: #Kerberos-wsfed:.+?Mean:\\s+(\\d+)#s
如果您采用文件格式,則可以僅使用空格或\\s
代替\\s+
。
值176
將位於匹配元素的第1組
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.