Regex 通知正则表达式,检查时间戳
我对微软的(正则表达式)表示怀疑。我有一个在这种模式下写入的日志文件Regex 通知正则表达式,检查时间戳,regex,c#,msdn,dngrep,Regex,C#,Msdn,Dngrep,我对微软的(正则表达式)表示怀疑。我有一个在这种模式下写入的日志文件 2017-02-20 15:58:45.442 - [XYZ] 155 RADIO_TRAIN_TO_TRACK_INITIATION_COM_SESSION .VAR1A: 155 .VAR2B .V
2017-02-20 15:58:45.442 - [XYZ] 155 RADIO_TRAIN_TO_TRACK_INITIATION_COM_SESSION
.VAR1A: 155
.VAR2B
.VAR3z
.VAR4t
2017-02-20 15:58:46.432 - [XYZ] 32 RADIO_TRACK_TO_TRAIN_CONFIGURATION_DETERMINATION
.VAR1A: 32
.VAR2Y
.VAR3s
.VAR4a
.VAR5w
.VAR6d
2017-02-20 15:58:48.541 - [XYZ] 156 RADIO_TRAIN_TO_TRACK_INITIATION_COM_SESSION
.VAR1A: 156
.NIDPACKET
.VAR3l
.VAR56
.VAR7b
.VAR100k
问题:我需要一个表达式来检查第一条消息[XYZ]155和第二条消息[XYZ]32是否连续,并且它们是否在10秒的间隔内
所以,我的问题是:足够解决这个问题吗?或者我会实现更多的代码(例如比较时间戳)
谢谢大家!但是我有一个C#中的应用程序,我将尝试添加您的代码来修改它
我的想法是:
1) 将所有文本提取到mex[XYZ]155,并将所有文本提取到mex[XYZ]156(包括时间戳)
2) 检查时间戳是否在10秒的间隔内。正确时返回OK,不正确时返回空溶液
关于第一点,我试试这个正则表达式
(?<=\[XYZ] \b155\b(?:(?!\n\d{4}-\d{2}-\d).)*?\n {3,}\.).*(?:\r?\n(?!\d{4}-\d{2}-\d).*)*|(?<=\[XYZ] \b156\b(?:(?!\n\d{4}-\d{2}-\d).)*?\n {3,}\.).*(?:\r?\n(?!\d{4}-\d{2}-\d).*)*
(?此:
尝试下面这样的代码,它不使用正则表达式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace ConsoleApplication49
{
class Program
{
const string FILENAME = @"c:\temp\test.txt";
static void Main(string[] args)
{
//DateTime endTime = DateTime.Now;
DateTime endTime = DateTime.Parse("2017-02-20 15:58:50");
DateTime startTime = endTime.AddSeconds(-10);
Session session = new Session(startTime, endTime);
session.ReadFile(FILENAME);
}
}
public class Session
{
public static List<Session> sessions = new List<Session>();
public static DateTime startTime { get; set; }
public static DateTime endTime { get; set; }
public DateTime time { get; set; }
public string name { get; set; }
public List<string> properties { get; set; }
public Session()
{
}
public Session(DateTime startTime, DateTime endTime)
{
Session.startTime = startTime;
Session.endTime = endTime;
}
public void ReadFile(string filename)
{
StreamReader reader = new StreamReader(filename);
string inputLine = "";
Session newSession = null;
Boolean skip = false;
while ((inputLine = reader.ReadLine()) != null)
{
inputLine = inputLine.Trim();
if (inputLine.Length > 0)
{
if (inputLine.StartsWith(".") && (skip == false))
{
newSession.properties.Add(inputLine);
}
if(!inputLine.StartsWith("."))
{
string[] array = inputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
DateTime time = DateTime.Parse(array[0] + ' ' + array[1]);
if ((time > startTime) && (time < endTime))
{
skip = false;
newSession = new Session();
newSession.properties = new List<string>();
Session.sessions.Add(newSession);
newSession.time = time;
newSession.name = array[5];
}
else
{
skip = true;
}
}
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
使用System.Text.RegularExpressions;
命名空间控制台应用程序49
{
班级计划
{
常量字符串文件名=@“c:\temp\test.txt”;
静态void Main(字符串[]参数)
{
//DateTime endTime=DateTime.Now;
DateTime endTime=DateTime.Parse(“2017-02-20 15:58:50”);
DateTime startTime=endTime.AddSeconds(-10);
会话=新会话(开始时间、结束时间);
session.ReadFile(文件名);
}
}
公开课
{
公共静态列表会话=新建列表();
公共静态日期时间开始时间{get;set;}
公共静态日期时间结束时间{get;set;}
公共日期时间{get;set;}
公共字符串名称{get;set;}
公共列表属性{get;set;}
公开会议()
{
}
公共会话(DateTime开始时间、DateTime结束时间)
{
Session.startTime=startTime;
Session.endTime=endTime;
}
公共void ReadFile(字符串文件名)
{
StreamReader=新的StreamReader(文件名);
字符串inputLine=“”;
会话newSession=null;
布尔跳跃=假;
而((inputLine=reader.ReadLine())!=null)
{
inputLine=inputLine.Trim();
如果(inputLine.Length>0)
{
if(inputLine.StartsWith(“.”&&(skip==false))
{
newSession.properties.Add(inputLine);
}
如果(!inputLine.StartsWith(“.”)
{
string[]数组=inputLine.Split(新字符[]{''},StringSplitOptions.RemoveEmptyEntries);
DateTime=DateTime.Parse(数组[0]+''+array[1]);
如果((时间>开始时间)和&(时间<结束时间))
{
跳过=错误;
newSession=新会话();
newSession.properties=新列表();
Session.sessions.Add(newSession);
newSession.time=时间;
newSession.name=数组[5];
}
其他的
{
跳过=真;
}
}
}
}
}
}
}
您可以使用Regex
提取时间戳…但是您必须对它们进行比较,可能会将它们转换为Timespan
@xanatos谢谢您,我怀疑。也许我认为应该实现更多的代码。@xanatos,您好,对我的编辑有什么建议吗?
var rx = new Regex(@"^ *([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}) - \[([^]]*)\] *([0-9]*)", RegexOptions.Multiline);
var matches = rx.Matches(str).Cast<Match>().Select(x => Tuple.Create(DateTime.Parse(x.Groups[1].Value, CultureInfo.InvariantCulture), x.Groups[2].Value, x.Groups[3].Value, x.Groups[4].Value)).ToArray();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace ConsoleApplication49
{
class Program
{
const string FILENAME = @"c:\temp\test.txt";
static void Main(string[] args)
{
//DateTime endTime = DateTime.Now;
DateTime endTime = DateTime.Parse("2017-02-20 15:58:50");
DateTime startTime = endTime.AddSeconds(-10);
Session session = new Session(startTime, endTime);
session.ReadFile(FILENAME);
}
}
public class Session
{
public static List<Session> sessions = new List<Session>();
public static DateTime startTime { get; set; }
public static DateTime endTime { get; set; }
public DateTime time { get; set; }
public string name { get; set; }
public List<string> properties { get; set; }
public Session()
{
}
public Session(DateTime startTime, DateTime endTime)
{
Session.startTime = startTime;
Session.endTime = endTime;
}
public void ReadFile(string filename)
{
StreamReader reader = new StreamReader(filename);
string inputLine = "";
Session newSession = null;
Boolean skip = false;
while ((inputLine = reader.ReadLine()) != null)
{
inputLine = inputLine.Trim();
if (inputLine.Length > 0)
{
if (inputLine.StartsWith(".") && (skip == false))
{
newSession.properties.Add(inputLine);
}
if(!inputLine.StartsWith("."))
{
string[] array = inputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
DateTime time = DateTime.Parse(array[0] + ' ' + array[1]);
if ((time > startTime) && (time < endTime))
{
skip = false;
newSession = new Session();
newSession.properties = new List<string>();
Session.sessions.Add(newSession);
newSession.time = time;
newSession.name = array[5];
}
else
{
skip = true;
}
}
}
}
}
}
}