0

ある種のログファイルを「解析」する正規表現を作成しようとしています。構造は次のようになります。

2013-09-05 00:01:14.5726 WEB Info [n/a: UPN Claim] New instance of service created.
2013-09-05 00:01:14.6038 WEB Info [n/a: UPN Claim] 
---------------- [ Ping received ] -------------
CurrentPrincipel has Claims:
Claim Type: http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name with Value: GROUP\User
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/primarysid with Value: S-1-5-21-36134387-561137642-176895030-23737
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/primarygroupsid with Value: S-1-5-21-36134387-561137642-174895030-513
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-21-36134387-561137642-176892330-513
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-1-0
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-32-545
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-2
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-11
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-15
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-21-36134387-561137642-326895030-16415
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-21-36134387-561137642-1732895030-31127
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-21-36134387-561137642-176235030-12815
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-21-36134387-561137642-176892330-12145
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-21-36134387-561137642-176895430-31228
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-21-36134387-561137642-176892330-16100
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-64-10

2013-09-05 00:01:15.0406 WEB Info [n/a: UPN Claim] New instance of service created.
2013-09-05 00:01:15.0718 WEB Info [n/a: UPN Claim] GetLangugesChanges invoked for rowVersion 1 and 8 clientChanges.

私の正規表現で達成したいのは、ログ エントリに対して 4 つの一致を取得することです (各エントリ = 1 つの一致)。次の正規表現を試しましたが、先頭のタイムスタンプがないと行を取得できません:

(^\d{4}-\d{2}-\d{2}(.|^|$)*(?=>^\d{4})*)

正規表現は次のように呼び出されます。

string input = File.ReadAllText(@"log.txt");
MatchCollection matches = Regex.Matches(input, @"(^\d{4}-\d{2}-\d{2}(.|^|$)*(?=>^\d{4})*)", RegexOptions.Multiline);
foreach (Match match in matches)
{
    Console.WriteLine("{0}", match.Value);
    Console.WriteLine("------");
}

与えられた出力は次のとおりです。

2013-09-05 00:01:14.5726 WEB Info [n/a: UPN Claim] New instance of service created. ------
2013-09-05 00:01:14.6038 WEB Info [n/a: UPN Claim]  ------
2013-09-05 00:01:15.0406 WEB Info [n/a: UPN Claim] New instance of service created. ------
2013-09-05 00:01:15.0718 WEB Info [n/a: UPN Claim] GetLangugesChanges invoked for rowVersion 1 and 8 clientChanges.
------

予想される出力は次のようになります。

2013-09-05 00:01:14.5726 WEB Info [n/a: UPN Claim] New instance of service created.
------
2013-09-05 00:01:14.6038 WEB Info [n/a: UPN Claim] 
---------------- [ Ping received ] -------------
CurrentPrincipel has Claims:
Claim Type: http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name with Value: GROUP\User
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/primarysid with Value: S-1-5-21-36134387-561137642-176895030-23737
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/primarygroupsid with Value: S-1-5-21-36134387-561137642-174895030-513
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-21-36134387-561137642-176892330-513
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-1-0
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-32-545
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-2
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-11
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-15
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-21-36134387-561137642-326895030-16415
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-21-36134387-561137642-1732895030-31127
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-21-36134387-561137642-176235030-12815
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-21-36134387-561137642-176892330-12145
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-21-36134387-561137642-176895430-31228
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-21-36134387-561137642-176892330-16100
Claim Type: http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid with Value: S-1-5-64-10
------
2013-09-05 00:01:15.0406 WEB Info [n/a: UPN Claim] New instance of service created.
------
2013-09-05 00:01:15.0718 WEB Info [n/a: UPN Claim] GetLangugesChanges invoked for rowVersion 1 and 8 clientChanges.
------

私は何を間違っていますか?どんな助けにも感謝します!

4

2 に答える 2

1

次のように、2 つの日付 (またはファイルの末尾) の間のすべてを一致させることができます。

^\d{4}-\d{2}-\d{2}.*?(?=^\d{4}-\d{2}-\d{2}|(?!.))

とともに使用しRegexOptions.Multiline | RegexOptions.Singlelineます。

別のオプションは、正規表現を使用して文字列を分割することです。

var regex = new Regex(@"^\d{4}-\d{2}-\d{2}", RegexOptions.Multiline);
string[] result = regex.Split(input);
于 2013-10-03T12:15:29.233 に答える
1

あなたの質問を正しく理解していれば、これが役立つはずです。

(?<=\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{4} )(.*)

例:

 2013-09-05 00:01:14.6038 VSWEB04 Info [n/a: UPN Claim] 

一致:

VSWEB04 Info [n/a: UPN Claim] 
于 2013-10-03T12:10:21.303 に答える