0

次の内容のログファイルを読んでいます

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

しかし、出力は Command: と Comment: の後にテキスト全体を取得していますが、ON の後のテキストは [FMC360Train_MSCRM].[MetadataSchema].[LocalizedLabel] を意味し、次のテキストは別の値として REORGANIZE になります。正規表現コードを変更するにはどうすればよいですかのような出力を得る

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

私のプログラムは長い出力を得るために働いていますが、上記の形式で出力したいので、正規表現についてのみ助けが必要です。

4

1 に答える 1

1

行を解析するための正確なルールはわかりませんCommandが、次の正規表現は 2 つの入力例で機能します。

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
于 2012-12-29T05:56:56.620 に答える