thekrakken / java-grok

Simple API that allows you to easily parse logs and other files
http://grok.nflabs.com/
Other
358 stars 152 forks source link

Matched json is blank in case any of one regex out multiple does not match #76

Open sudhirkumar1980 opened 6 years ago

sudhirkumar1980 commented 6 years ago

java grok is return matched json as blank when one regex out of multple does not match. Example grok = Grok.create("patterns/patterns"); grok.compile("%{NUMBER:hits} %{USER:word}"); String s = "234\n"; Match gm = grok.match(s); gm.captures(); System.out.println(gm.toJson());

in this case only %{NUMBER:hits} matches and not %{USER:word}. I expect java grok to return json {"hits":234} but it is returning {}.

retoo commented 6 years ago

try:

 grok.compile("%{NUMBER:hits}(?: %{USER:word})?");

if tell grok to match "numer word" then it will expect that there's a number and then a word. The word is not optional.