2014-06-06 58 views
0

https://github.com/thekrakken/java-grok爲什麼java-grok在字符串不匹配時返回最後一次成功匹配?

我正在使用Grok API for Java。代碼如下:

Grok grok = Grok.EMPTY; 

    // add a pattern to grok 
    grok.addPatternFromFile("pat.txt"); 

    // compile and add semantic 
    grok.compile("%{NUMBER:hits} %{USER:word}"); 

      String str = "234 wdfd\n"; 
    Match m = grok.match(str); 
    m.captures(); 

    // Print 
    System.out.println(m.toJson()); 


    grok = Grok.EMPTY; 
    str = "ssdfsdf\n"; 
    Match m2 = grok.match(str); 
    m2.captures(); 

    System.out.println(m2.toJson()); 

的程序的輸出是:

  {"hits":234,"word":"wdfd"} 
      {"hits":234,"word":"wdfd"} 

我清空神交例如,我使用單獨的「M2」匹配變量,但仍然程序返回上次成功匹配而不是NULL或者可以通知我匹配失敗的錯誤。我怎麼知道比賽失敗的時候?

+0

你確定你重新編譯你的代碼? – chrylis

+1

是的,看起來當沒有比賽的時候,最後一場比賽就交付了。嘗試調試到源代碼。 **聯繫他們。** –

回答

0

這實際上是一個小錯誤(由於一個poc),我解決了這個問題。

現在未加整行的行將返回空。

例如:

Grok grok = Grok.create("pat.txt"); 
// compile and add semantic 
grok.compile("%{NUMBER:hits} %{USER:word}"); 

String str = "234 wdfd\n"; 
Match m = grok.match(str); 
m.captures(); 

// Print 
System.out.println(m.toJson()); 


// Here you dont need to create a new instance of grok [grok = Grok.EMPTY;] 
str = "ssdfsdf\n"; 
// here you can reuse the matcher if you want. 
Match m2 = grok.match(str); 
m2.captures(); 

System.out.println(m2.toJson()); 

程序的輸出現在是:

{"hits":234,"word":"wdfd"} 
{} 

希望其幫助

相關問題