简体   繁体   English

正则表达式与文本C#匹配?

[英]Regular expression to match with text C#?

I'm reading a log file which has following contents 我正在读取一个包含以下内容的日志文件

DateTime: 2012-12-09 17:00:18
Command: ALTER INDEX [XPKAttribute] ON [FMC360Train_MSCRM].[MetadataSchema].[Attribute] REORGANIZE WITH (LOB_COMPACTION = ON)
Comment: ObjectType: Table, IndexType: Clustered, ImageText: No, NewLOB: No, FileStream: No, AllowPageLocks: Yes, PageCount: 1336, Fragmentation: 10.1796
Outcome: Succeeded
Duration: 00:00:01
DateTime: 2012-12-09 17:00:19

DateTime: 2012-12-09 17:00:19
Command: ALTER INDEX [XPKLocalizedLabel] ON [FMC360Train_MSCRM].[MetadataSchema].[LocalizedLabel] REORGANIZE WITH (LOB_COMPACTION = ON)
Comment: ObjectType: Table, IndexType: Clustered, ImageText: No, NewLOB: Yes, FileStream: No, AllowPageLocks: Yes, PageCount: 2522, Fragmentation: 18.5964
Outcome: Succeeded
Duration: 00:00:01
DateTime: 2012-12-09 17:00:20

Here the regex code I'm using 这是我正在使用的正则表达式代码

var data = new Regex(@"^DateTime:\ (?<StartTime>[^\r]+)\r\nCommand: ALTER INDEX\ (?<Command>[^\r]+)\r\nComment:\ (?<Comment>[^\r]+)\r\nOutcome:\ (?<Outcome>[^\r]+)\r\nDuration:\ (?<Duration>[^\r]+)\r\nDateTime:\ (?<EndTime>[^\r]+)", RegexOptions.Multiline)
                .Matches(File.ReadAllText(@"C:\Users\dalvi\Desktop\Index Logs\trial.txt")).Cast<Match>().Select(m => new
                {
                    StartTime = m.Groups["StartTime"].Value,
                    Command = m.Groups["Command"].Value,
                    Comment = m.Groups["Comment"].Value,
                    Outcome = m.Groups["Outcome"].Value,
                    Duration = m.Groups["Duration"].Value,
                    EndTime = m.Groups["EndTime"].Value
                });

But the output I'm getting whole text after Command: and Comment: but I want text after ON means [FMC360Train_MSCRM].[MetadataSchema].[LocalizedLabel] and then the next text REORGANIZE as different value, how can i modify the regex code to get output like 但是我在Command:和Comment之后得到的是全文输出,但是我想在ON之后输入文本意味着[FMC360Train_MSCRM]。[MetadataSchema]。[LocalizedLabel],然后将下一个文本重新组织为不同的值,我该如何修改正则表达式代码得到像

StartTime   DBTableName Type    Comment Outcome Duration    EndTime
2012-12-09 17:00:18 [FMC360Train_MSCRM].[MetadataSchema].[Attribute]    REORGANIZE  PageCount: 1336 Succeeded   00:00:01    2012-12-09 17:00:19

My program is working to get a long output but I want output in above format so i need help only on regex. 我的程序正在努力获取较长的输出,但我希望以上述格式输出,因此我仅在正则表达式上需要帮助。

I don't know the exact rules for parsing your Command line, but the following regular expression works with your two example inputs: 我不知道解析Command行的确切规则,但是以下正则表达式可用于您的两个示例输入:

var data = new Regex(@"^DateTime:\ (?<StartTime>[^\r]+)\r\nCommand: ALTER INDEX \[([^\]]+)\] ON (?<DBTableName>\[([^\]]+)\]\.\[([^\]]+)\]\.\[([^\]]+)\]) (?<Type>[^ ]+) ([^\r]+)\r\nComment:\ (?<Comment>.+Fragmentation: (?<Fragmentation>[\d\.]+)[^\r]+)\r\nOutcome:\ (?<Outcome>[^\r]+)\r\nDuration:\ (?<Duration>[^\r]+)\r\nDateTime:\ (?<EndTime>[^\r]+)", RegexOptions.Multiline)
    .Matches(File.ReadAllText(@"C:\Users\dalvi\Desktop\Index Logs\trial.txt")).Cast<Match>().Select(m => new
        {
            StartTime = m.Groups["StartTime"].Value,
            DBTableName = m.Groups["DBTableName"].Value,
            Type = m.Groups["Type"].Value,
            Comment = m.Groups["Comment"].Value,
            Fragmentation = m.Groups["Fragmentation"].Value,
            Outcome = m.Groups["Outcome"].Value,
            Duration = m.Groups["Duration"].Value,
            EndTime = m.Groups["EndTime"].Value
        });

EDIT: 编辑:

Below is a full console application running on your test data: 下面是运行在测试数据上的完整控制台应用程序:

class Program
{
    static void Main(string[] args)
    {
        string input =
            @"DateTime: 2012-12-09 17:00:18
Command: ALTER INDEX [XPKAttribute] ON [FMC360Train_MSCRM].[MetadataSchema].[Attribute] REORGANIZE WITH (LOB_COMPACTION = ON)
Comment: ObjectType: Table, IndexType: Clustered, ImageText: No, NewLOB: No, FileStream: No, AllowPageLocks: Yes, PageCount: 1336, Fragmentation: 10.1796
Outcome: Succeeded
Duration: 00:00:01
DateTime: 2012-12-09 17:00:19

DateTime: 2012-12-09 17:00:19
Command: ALTER INDEX [XPKLocalizedLabel] ON [FMC360Train_MSCRM].[MetadataSchema].[LocalizedLabel] REORGANIZE WITH (LOB_COMPACTION = ON)
Comment: ObjectType: Table, IndexType: Clustered, ImageText: No, NewLOB: Yes, FileStream: No, AllowPageLocks: Yes, PageCount: 2522, Fragmentation: 18.5964
Outcome: Succeeded
Duration: 00:00:01
DateTime: 2012-12-09 17:00:20";

        var data = new Regex(@"^DateTime:\ (?<StartTime>[^\r]+)\r\nCommand: ALTER INDEX \[([^\]]+)\] ON (?<DBTableName>\[([^\]]+)\]\.\[([^\]]+)\]\.\[([^\]]+)\]) (?<Type>[^ ]+) ([^\r]+)\r\nComment:\ (?<Comment>.+Fragmentation: (?<Fragmentation>[\d\.]+)[^\r]+)\r\nOutcome:\ (?<Outcome>[^\r]+)\r\nDuration:\ (?<Duration>[^\r]+)\r\nDateTime:\ (?<EndTime>[^\r]+)", RegexOptions.Multiline)
            .Matches(input).Cast<Match>().Select(m => new
            {
                StartTime = m.Groups["StartTime"].Value,
                DBTableName = m.Groups["DBTableName"].Value,
                Type = m.Groups["Type"].Value,
                Comment = m.Groups["Comment"].Value,
                Fragmentation = m.Groups["Fragmentation"].Value,
                Outcome = m.Groups["Outcome"].Value,
                Duration = m.Groups["Duration"].Value,
                EndTime = m.Groups["EndTime"].Value
            });

        foreach (var datum in data)
        {
            Console.WriteLine("StartTime: {0}", datum.StartTime);
            Console.WriteLine("DBTableName: {0}", datum.DBTableName);
            Console.WriteLine("Type: {0}", datum.Type);
            Console.WriteLine("Comment: {0}", datum.Comment);
            Console.WriteLine("Fragmentation: {0}", datum.Fragmentation);
            Console.WriteLine("Outcome: {0}", datum.Outcome);
            Console.WriteLine("Duration: {0}", datum.Duration);
            Console.WriteLine("EndTime: {0}", datum.EndTime);
            Console.WriteLine();
        }

        Console.ReadKey();
    }
}

And this is its ouput: 这是它的输出:

StartTime: 2012-12-09 17:00:18
DBTableName: [FMC360Train_MSCRM].[MetadataSchema].[Attribute]
Type: REORGANIZE
Comment: ObjectType: Table, IndexType: Clustered, ImageText: No, NewLOB: No, FileStream: No, AllowPageLocks: Yes, PageCount: 1336, Fragmentation: 10.1796
Fragmentation: 10.179
Outcome: Succeeded
Duration: 00:00:01
EndTime: 2012-12-09 17:00:19

StartTime: 2012-12-09 17:00:19
DBTableName: [FMC360Train_MSCRM].[MetadataSchema].[LocalizedLabel]
Type: REORGANIZE
Comment: ObjectType: Table, IndexType: Clustered, ImageText: No, NewLOB: Yes, FileStream: No, AllowPageLocks: Yes, PageCount: 2522, Fragmentation: 18.5964
Fragmentation: 18.596
Outcome: Succeeded
Duration: 00:00:01
EndTime: 2012-12-09 17:00:20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM