Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
要与文本C#匹配的正则表达式?_C#_.net_Regex - Fatal编程技术网

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

要与文本C#匹配的正则表达式?,c#,.net,regex,C#,.net,Regex,我正在读取一个日志文件,其中包含以下内容 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:

我正在读取一个日志文件,其中包含以下内容

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\ (?<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
                });
我的程序正在工作,以获得一个长输出,但我想在上面的格式输出,所以我需要帮助
仅在正则表达式上。

我不知道解析
命令行的确切规则,但以下正则表达式适用于两个示例输入:

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
        });

使用像开发正则表达式这样的工具,并根据输入轻松测试它。@ViswanathanIyer我无法在测试数据上重现这个问题。请参见上面的测试应用程序。我正在努力获取碎片的值,您能否告诉我如何从我的文件中获取碎片数组中的值。@ViswanathanIyer我也在回答中添加了碎片解析。
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
        });
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();
    }
}
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