我有所有的行textLines1
,我想计算所有这些行的平均经过时间。我已经为此尝试过正则表达式。但它给了我错误的计算。
日志文件格式:
INFO: WX ADVSearch = Server:testserver Entity:BUG User:acucu Elapsed Time:274ms
INFO: WX ADVSearch = Server:testserver Entity:BUG User:acucu Elapsed Time:274ms
我试过的代码是:
List<string> textLines1 = new List<string>(users);
string x = string.Join(",", textLines1);
Regex regex = new Regex(@"Elapsed Time:\s*(?<value>\d+\.?\d*)\s*ms");
Match match = regex.Match(x);
double totalTime = 0;
int count = 0;
foreach (string line in textLines1)
{
if (match.Captures.Count > 0)
{
try
{
count++;
double time = Double.Parse(match.Groups["value"].Value);
totalTime += time;
}
catch (Exception)
{
// no number
}
}
}
double average = totalTime / count;
Console.WriteLine("ADVAverage=" + average);