.NET RegEx:查找函数并验证

在该项目中,我目前正在研究在触发事件中捕获来自各种来源的信号。为了分析信号,我们想要使用一些查询并为我们的信号提出命名约定。我们希望使用 RegEx 来验证一致性以及拆分信号字符串的各个部分。


信号需要具有以下结构:


Function(SignalName@Time)

Function 和 @Time 是可选的,但 SignalName 始终是必需的。


一些示例:


Corp.Signals.LivingRoom.Temperature@TStart   //temperature of living room at start of event

Corp.Signals.LivingRoom.Temperature          //missing time stamp will default to start of event

int(Corp.Signals.BedRoom.NoiseLevel@T+4.20)  //integral of bed room noise from Start to 4.20 seconds into the event

Corp.Signals.Kitchen.Light@TEnd-.5           //state of the kitchen light 0.5 seconds before the end of the event

到目前为止我想出了这个正则表达式:


(?<open>(?<function>\w+)\(|)(?<signal>Corp\.Signals\.\w+\.\w+)(?:@T(?<time>((Start|Mid|End|)([-+]\d?(\.\d+)?)?))|)(?<-open>\)|)

到目前为止,这工作得很好,但仍然匹配不属于命名方案的字符串。


没有功能但末尾带有右括号的信号:


Corp.Signals.BedRoom.NoiseLevel@T+4)


我不确定为什么会消耗它,因为“开放”平衡组没有捕获任何内容。最后的parenteses怎么能消耗掉呢?


具有函数但缺少右括号的信号。


int(Corp.Signals.LivingRoom.Temperature@T-.2


仅具有部分时间戳的信号(缺少时间)


Corp.Signals.LivingRoom.Temperature@T+

RegEx Storm 示例


杨__羊羊
浏览 106回答 2
2回答

不负相思意

您可能会做的是使用交替来匹配具有函数和左括号和右括号的部分,或者匹配不匹配可选时间的信号的部分。在交替中,您可以使用 C# 支持的相同组名称。(?<!\S)(?:(?<function>\w+)\((?<signal>Corp\.Signals(?:\.\w+)*)(?:@T(?<time>(?:Start|Mid|End)?(?:[-+](?:\d+(?:\.\d+)?)?|\.\d+))?)?\)|(?<signal>Corp\.Signals(?:\.\w+)*)(?:@T(?<time>(?:Start|Mid|End)?(?:[-+](?:\d+(?:\.\d+)?|\.\d+))?)?)?)(?!\S)在很大程度上可以看到命名组function,signal以及time(?<!\S)断言左边的不是非空白字符(?:非捕获组@T(?<time>(?:Start|Mid|End)?(?:[-+](?:\d+(?:\.\d+)?)?|\.\d+))?(?<function>\w+)\(匹配左括号(?<signal>Corp\.Signals(?:\.\w+)*)(?:非捕获组)?关闭非捕获组并使其可选\)匹配右括号|或者(?<signal>Corp\.Signals(?:\.\w+)*)(?:@T(?<time>(?:Start|Mid|End)?(?:[-+](?:\d+(?:\.\d+)?|\.\d+))?)?)?与前面的部分相同,没有函数和括号)(?!\S)断言右边的不是非空白字符正则表达式演示

梵蒂冈之花

尝试以下操作:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string[] inputs = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Corp.Signals.LivingRoom.Temperature@TStart",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "int(Corp.Signals.BedRoom.NoiseLevel@T+4)",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Corp.Signals.LivingRoom.Temperature@TMid+2.3",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Corp.Signals.LivingRoom.Temperature",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Corp.Signals.LivingRoom.Temperature@T+",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Corp.Signals.BedRoom.NoiseLevel@T+4)",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "int(Corp.Signals.LivingRoom.Temperature@T-.2"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Function(SignalName@Time) sample, Function and Time optional&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string pattern = @"^((\w[\w.]+(@T(([+-](\d+|\d*.\d+))|\w+))?)|(\w+\(\w[\w.]+(@T(([+-](\d+|\d*.\d+))|\w+))?\)))$";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (string input in inputs)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Match match = Regex.Match(input, pattern);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Success : {0}, Expression : '{1}'", match.Success, input);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();
打开App,查看更多内容
随时随地看视频慕课网APP