找到下一个模式的PCRE正则表达式匹配模式

时间:2017-05-10 17:36:36

标签: regex pcre

我正在努力寻找这个正则表达式的解决方案,这似乎是相当直接的。我需要匹配一个在另一个匹配模式之前的模式。

我需要捕捉以下“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之后找到Mean的值但是有困难。谢谢你的帮助。

3 个答案:

答案 0 :(得分:2)

使用正则表达式

Kerberos-wsfed[\s\S]*?Mean: *(\d+)

平均值包含在捕获组1中,即$1\1,具体取决于您的编程语言。

请参阅demo

答案 1 :(得分:0)

使用捕获组:

Kerberos-wsfed:[\s\S]*Mean:\s(\d+)
  • Kerberos-wsfed:与文字原样匹配
  • [\s\S]*允许(包括行分隔符)
  • 之间的任意数量的字符
  • Mean:\s与文字Mean匹配,后跟空格\s
  • 最后(\d+)包含在第一个捕获组中,捕获您要查找的值。它基本上允许任意数量的数字

Regex 101 Demo

您要查找的值(176)将位于第一个捕获组$1或第一个基于您的语言的捕获组。例如,在PHP中:

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

echo $matches[0][1];
// Output: 176

答案 2 :(得分:0)

尝试使用常规表达式:#Kerberos-wsfed:.+?Mean:\s+(\d+)#s

如果您以文件格式保密,则可以只使用空格或\s代替\s+

176将位于匹配元素的第1组

演示:https://regex101.com/r/gwkUPJ/1