0

テキストファイルにいくつかの文字列があるかどうかを単純にチェックする方法がありますが、テキストドキュメントに存在するにもかかわらず、いくつかの文字列が一致として選択されていません。この例では見つからない問題のある文字列を含めました。

static void Main(string[] args)
{
    string str = @"1009 10.32.0.28 03/05/2012 09:11:48 The license expires in 1192 day(s).";
        StreamReader sr = new StreamReader(@"event_history.txt");
        string allRead = sr.ReadToEnd();
        sr.Close();
        string regMatch = str;
        if (Regex.IsMatch(allRead, regMatch))
        {
            Console.WriteLine("found\n");
        }
        else
        {
            Console.WriteLine("not found\n");
        }

        Console.ReadKey();

}

event_history.txt

1009 10.32.0.28 03/05/2012 09:11:48 The license expires in 1192 day(s).

正規表現の一致を「testing」と置き換えてから、「testing」をテキストファイルに追加すると、問題なく一致として取得されます:S

4

4 に答える 4

1

regMatch=str であり、str は正規表現ではありません。なぜ正規表現を使用しているのですか? 次のようなものを使用する必要があります

".*The license expires in [0-9]* day(s).".

さらに、IP 10.23.0.28 のすべてのエントリについて:

"1009 10\.32\.0\.28 ..\/..\/.... ..\:..\:.. The license expires in [0-9]* day(s)."

たとえば、ファイル regex.txt を使用します。

$ cat regex.txt
1009 10.32.0.28 03/05/2012 09:11:48 The license expires in 1192 day(s).
1009 10.32.0.28 04/05/2012 09:11:48 The license expires in 1192 day(s).
1009 10.32.0.29 04/05/2012 09:11:48 The license expires in 1192 day(s).
1009 10.32.0.30 04/05/2012 09:11:48 The license expires in 1192 day(s).

結果は次のとおりです。

$ grep "1009 10\.32\.0\.28 ..\/..\/.... ..\:..\:.. The license expires in [0-9]* day(s)." regex.txt
1009 10.32.0.28 03/05/2012 09:11:48 The license expires in 1192 day(s).
1009 10.32.0.28 04/05/2012 09:11:48 The license expires in 1192 day(s).

それが常にチェックしたい文字列である場合(1009、1192日、IPアドレスと日付は常に静的)。

使用する:

".... 10\.32\.0\.28 04\/05\/2012 ..\:..\:.. The license expires in 1192 day(s)."
于 2012-05-03T14:34:36.907 に答える
1

これstrは、達成しようとしているものの正しい正規表現ではありません。たとえば、.任意の文字とその周りのかっこsはグループ化されており、キャプチャされないことを意味します。実際には、それが常にチェックしたい文字列であるかどうかを確認する必要があります(1009、1192日、IPアドレスと日付は常に静的です)allReadstr

string str = @"1009 10.32.0.28 03/05/2012 09:11:48 The license expires in 1192 day(s).";
StreamReader sr = new StreamReader(@"event_history.txt");
string allRead = sr.ReadToEnd();

if(allRead.Contains(str))
{
    Console.WriteLine("found\n");
}
else
{
    Console.WriteLine("not found\n");
}

非静的値をキャプチャする正規表現を探している場合は、ここにアクセスしてください

string str = @"\d+ \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} \d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2} The license expires in \d+ day\(s\)\.";
于 2012-05-03T14:37:18.247 に答える
0

Regex.IsMatchとして使用していString.Containsます。正規表現には独自の構文があるため、(一般的には) 機能しません。

sあなたの特定の例では、正規表現エンジンは現在書かれているようにそれらと一致しないため、括弧をエスケープする必要があります。それらはキャプチャ演算子です。皮肉なことに、正規表現のドットはドットと一致しますが、ドットもエスケープする必要があります。そう:

string str = @"1009 10\.32\.0\.28 03/05/2012 09:11:48 The license expires in 1192 day\(s\)\.";
于 2012-05-03T14:36:52.843 に答える
0

正規表現が一致しない理由は、( および ) が正規表現の特殊文字であるためです。これらは、後で参照する値であるグループ化を表します。それらを通常の文字だけに変更するには、それらの前に \ を追加します。したがって、正規表現は次のようになります

@"1009 10.32.0.28 03/05/2012 09:11:48 The license expires in 1192 day\(s\)."
于 2012-05-03T14:41:02.140 に答える