正则表达式提取物可选的组 [英] Regex extract optional group

查看:219
本文介绍了正则表达式提取物可选的组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在格式一些日志字符串:



T01:警告:Tag1中:信息



T23:与Tag2:消息2



我试图提取在 T 号,检测警告的存在:,然后将标签和消息都在同一个正则表达式的文本。 警告:的强制性要求。被绊倒了我,虽然

 私人常量字符串RegexExpression = @^ T(LT? ;数> \d +):(?<警告>警告:)(小于标签> [^:]?+):(小于;消息> *?)。 
私人常量字符串消息=blar blar blar:某些消息话题;

//这个测​​试工作
[TestMethod的]
公共无效RegExMatchByTwoNamedGroupsWarningTest()
{
变种雷克斯=新的正则表达式(RegexExpression);
常量字符串wholePacket =T12:警告:logtag:+消息;
VAR匹配= rex.Match(wholePacket);
Assert.IsTrue(match.Groups [警告]的成功。); //警告存在
Assert.IsTrue(match.Success);
Assert.IsTrue(match.Groups [编号]成功);
Assert.AreEqual(12,match.Groups [数量]值。);
Assert.IsTrue(match.Groups [标签]的成功。);
Assert.AreEqual(logtag,match.Groups [标签]值。);
Assert.IsTrue(match.Groups [消息]的成功。);
Assert.AreEqual(消息,match.Groups [消息]值。);
}

[TestMethod的]
公共无效RegExMatchByTwoNamedGroupsNoWarningTest()
{
变种雷克斯=新的正则表达式(RegexExpression);
常量字符串wholePacket =T12:logtag:+消息;
VAR匹配= rex.Match(wholePacket);
Assert.IsFalse(match.Groups [警告]的成功。); //警告缺少
Assert.IsTrue(match.Success); //失败
Assert.IsTrue(match.Groups [编号]的成功。); //失败
Assert.AreEqual(12,match.Groups [数量]值。);
Assert.IsTrue(match.Groups [标签]的成功。); //失败
Assert.AreEqual(logtag,match.Groups [标签]值。);
Assert.IsTrue(match.Groups [消息]的成功。); //失败
Assert.AreEqual(消息,match.Groups [消息]值。);
}


解决方案

您的问题是在空白你的正则表达式。



如果报警组是不是有那么它试图将空间从可选警告模式,并从后一前匹配。显然,你只想匹配其中之一。



的解决方案是为具有可选的图案内的空间中的一个与警告一起。即:

  ^ T(小于号> \d +?)(?<警告>警告:)(? ?<标记和GT; [^:] +):(<消息> *)


I have some log strings in the format:

T01: Warning: Tag1: Message

T23: Tag2: Message2

I am trying to extract the T number, detect the presence of Warning:, then text of the Tag and Message all in one regex. The optional requirement of "Warning:" is tripping me up though.

    private const string RegexExpression = @"^T(?<Number>\d+): (?<Warning>Warning:)? (?<Tag>[^:]+): (?<Message>.*)";
    private const string Message = "blar blar blar: some messsage";

    //this test works
    [TestMethod]
    public void RegExMatchByTwoNamedGroupsWarningTest()
    {
        var rex = new Regex(RegexExpression);
        const string wholePacket = "T12: Warning: logtag: " + Message;
        var match = rex.Match(wholePacket);
        Assert.IsTrue(match.Groups["Warning"].Success); //warning is present
        Assert.IsTrue(match.Success);
        Assert.IsTrue(match.Groups["Number"].Success);
        Assert.AreEqual("12", match.Groups["Number"].Value);
        Assert.IsTrue(match.Groups["Tag"].Success);
        Assert.AreEqual("logtag", match.Groups["Tag"].Value);
        Assert.IsTrue(match.Groups["Message"].Success);
        Assert.AreEqual(Message, match.Groups["Message"].Value);
    }

    [TestMethod]
    public void RegExMatchByTwoNamedGroupsNoWarningTest()
    {
        var rex = new Regex(RegexExpression);
        const string wholePacket = "T12: logtag: " + Message;
        var match = rex.Match(wholePacket);
        Assert.IsFalse(match.Groups["Warning"].Success); //warning is missing
        Assert.IsTrue(match.Success); //fails
        Assert.IsTrue(match.Groups["Number"].Success); //fails
        Assert.AreEqual("12", match.Groups["Number"].Value);
        Assert.IsTrue(match.Groups["Tag"].Success); //fails
        Assert.AreEqual("logtag", match.Groups["Tag"].Value);
        Assert.IsTrue(match.Groups["Message"].Success); //fails
        Assert.AreEqual(Message, match.Groups["Message"].Value);
    }

解决方案

Your problem is the whitespace in your regex.

If the warning group is not there then it is trying to match the space from before the optional warning pattern and the one from after. Clearly you only want to match one of them.

The solution is to have one of the spaces inside the optional pattern along with the warning. ie:

^T(?<Number>\d+): (?<Warning>Warning: )?(?<Tag>[^:]+): (?<Message>.*)

这篇关于正则表达式提取物可选的组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆