RegexUtils { public static String extract(CharSequence line, Pattern pattern, Collection<Integer> groupsToKeep, String separator, RegexTransformer transformer) { StringBuilder bldr = new StringBuilder(); extract(line, bldr, pattern, groupsToKeep, separator, transformer); return bldr.toString(); } private  RegexUtils(); static String extract(CharSequence line, Pattern pattern, Collection<Integer> groupsToKeep,
                               String separator, RegexTransformer transformer); static void extract(CharSequence line, StringBuilder outputBuffer,
                             Pattern pattern, Collection<Integer> groupsToKeep, String separator,
                             RegexTransformer transformer); static final RegexTransformer IDENTITY_TRANSFORMER; static final RegexFormatter IDENTITY_FORMATTER; }
@Test public void testExtract() throws Exception { Pattern pattern = Pattern.compile("(?<=(\\?|&)q=).*?(?=&|$)"); String line = "127.0.0.1 - - [24/05/2010:01:19:22 +0000] \"GET /solr/select?q=import statement&start=1 HTTP/1.1\" 200 37571"; String res = RegexUtils.extract(line, pattern, Collections.<Integer>emptyList(), " ", RegexUtils.IDENTITY_TRANSFORMER); assertEquals(res, "import statement", res); for (int i = 0; i < TEST_STRS.length; i++) { String testStr = TEST_STRS[i]; res = RegexUtils.extract(testStr, pattern, Collections.<Integer>emptyList(), " ", new URLDecodeTransformer()); assertEquals(GOLD[i], res); } pattern = Pattern.compile("((?<=(\\?|&)q=)(.*?)(?=(&|$))|(?<=((\\?|&)start=))(\\d+))"); res = RegexUtils.extract(line, pattern, Collections.<Integer>emptyList(), " ", RegexUtils.IDENTITY_TRANSFORMER); assertEquals(res, "import statement 1", res); pattern = Pattern.compile("(start=1) HTTP"); Collection<Integer> groupsToKeep = Lists.newArrayList(); groupsToKeep.add(1); res = RegexUtils.extract(line, pattern, groupsToKeep, " ", RegexUtils.IDENTITY_TRANSFORMER); assertEquals(res, "start=1", res); }
@Test public void testExtract() throws Exception { Pattern pattern = Pattern.compile("(?<=(\\?|&)q=).*?(?=&|$)"); String line = "127.0.0.1 - - [24/05/2010:01:19:22 +0000] \"GET /solr/select?q=import statement&start=1 HTTP/1.1\" 200 37571"; String res = RegexUtils.extract(line, pattern, Collections.<Integer>emptyList(), " ", RegexUtils.IDENTITY_TRANSFORMER); assertEquals(res, "import statement", res); for (int i = 0; i < TEST_STRS.length; i++) { String testStr = TEST_STRS[i]; res = RegexUtils.extract(testStr, pattern, Collections.<Integer>emptyList(), " ", new URLDecodeTransformer()); assertEquals(GOLD[i], res); } pattern = Pattern.compile("((?<=(\\?|&)q=)(.*?)(?=(&|$))|(?<=((\\?|&)start=))(\\d+))"); res = RegexUtils.extract(line, pattern, Collections.<Integer>emptyList(), " ", RegexUtils.IDENTITY_TRANSFORMER); assertEquals(res, "import statement 1", res); pattern = Pattern.compile("(start=1) HTTP"); Collection<Integer> groupsToKeep = new ArrayList<>(); groupsToKeep.add(1); res = RegexUtils.extract(line, pattern, groupsToKeep, " ", RegexUtils.IDENTITY_TRANSFORMER); assertEquals(res, "start=1", res); }