Skip to content
This repository was archived by the owner on Jun 2, 2022. It is now read-only.

Commit 8610b91

Browse files
Merge pull request thekrakken#1 from Leemoonsoo/master
Match.toMap() to get match in java Map.
2 parents 436179a + a2a4dc0 commit 8610b91

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/main/java/com/nflabs/Grok/Match.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ public String toJson(){
120120

121121
}
122122

123+
/**
124+
*
125+
* @return java map object from the matched element in the text
126+
*/
127+
public Map<String, String> toMap(){
128+
return _capture;
129+
}
130+
123131
/**
124132
*
125133
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.nflabs.Grok;
2+
3+
import java.util.Map;
4+
5+
import junit.framework.TestCase;
6+
7+
public class GrokTest extends TestCase {
8+
9+
protected void setUp() throws Exception {
10+
super.setUp();
11+
}
12+
13+
protected void tearDown() throws Exception {
14+
super.tearDown();
15+
}
16+
17+
/**
18+
* Do some basic test
19+
*
20+
* @throws Throwable
21+
*/
22+
public void testGrok() throws Throwable{
23+
Grok g = new Grok();
24+
25+
g.addPatternFromFile("patterns/base");
26+
27+
g.compile("%{URI}");
28+
29+
Match gm = g.match("http://www.google.com/search=lol");
30+
gm.captures();
31+
32+
Map<String, String> map = gm.toMap();
33+
assertEquals("www.google.com", map.get("HOSTNAME"));
34+
assertEquals("www.google.com", map.get("host"));
35+
assertEquals("http", map.get("proto"));
36+
assertEquals(null, map.get("port"));
37+
assertEquals("/search=lol", map.get("params"));
38+
}
39+
}

0 commit comments

Comments
 (0)