{"action":"created","comment":{"url":"https://api.github.com/repos/metamx/java-util/pulls/comments/44208619","id":44208619,"diff_hunk":"@@ -0,0 +1,124 @@\n+/*\n+ * Copyright 2011,2012 Metamarkets Group Inc.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ *     http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+package com.metamx.common.parsers;\n+\n+import com.google.common.base.Function;\n+import com.google.common.base.Optional;\n+import com.google.common.base.Splitter;\n+import com.google.common.collect.Iterables;\n+import com.google.common.collect.Lists;\n+import com.metamx.common.collect.Utils;\n+import com.metamx.common.logger.Logger;\n+\n+import java.util.ArrayList;\n+import java.util.List;\n+import java.util.Map;\n+import java.util.regex.Matcher;\n+import java.util.regex.Pattern;\n+\n+/**\n+ */\n+public class RegexParser implements Parser<String, Object>\n+{\n+  private static final Logger log = new Logger(RegexParser.class);\n+\n+  private final String pattern;\n+  private final String listDelimiter;\n+  private final Splitter listSplitter;\n+  private final Function<String, Object> valueFunction;\n+\n+  private ArrayList<String> fieldNames = null;\n+\n+  public RegexParser(\n+      final String pattern,\n+      final Optional<String> listDelimiter\n+  )\n+  {\n+    this.pattern = pattern;\n+    this.listDelimiter = listDelimiter.isPresent() ? listDelimiter.get() : Parsers.DEFAULT_LIST_DELIMITER;\n+    this.listSplitter = Splitter.on(this.listDelimiter);\n+    this.valueFunction = new Function<String, Object>()\n+    {\n+      @Override\n+      public Object apply(String input)\n+      {\n+        if (input.contains(RegexParser.this.listDelimiter)) {\n+          return Lists.newArrayList(\n+              Iterables.transform(\n+                  listSplitter.split(input),\n+                  ParserUtils.nullEmptyStringFunction\n+              )\n+          );\n+        } else {\n+          return ParserUtils.nullEmptyStringFunction.apply(input);\n+        }\n+      }\n+    };\n+  }\n+\n+  public RegexParser(\n+      final String pattern,\n+      final Optional<String> listDelimiter,\n+      final Iterable<String> fieldNames\n+  )\n+  {\n+    this(pattern, listDelimiter);\n+\n+    setFieldNames(fieldNames);\n+  }\n+\n+  @Override\n+  public Map<String, Object> parse(String input)\n+  {\n+    try {\n+      Pattern compiled = Pattern.compile(pattern);\n+      Matcher matcher = compiled.matcher(input);\n+\n+      if (!matcher.matches()) {\n+        throw new ParseException(\"Incorrect Regex: %s . No match found.\", pattern);\n+      }\n+\n+      List<String> values = Lists.newArrayList();\n+      for (int i = 0; i < matcher.groupCount(); i++) {\n+        values.add(matcher.group(i + 1));\n+      }","path":"src/main/java/com/metamx/common/parsers/RegexParser.java","position":98,"original_position":98,"commit_id":"6f68c7dbec429aa4f1152c531e09399ecba1b9b5","original_commit_id":"6f68c7dbec429aa4f1152c531e09399ecba1b9b5","user":{"login":"himanshug","id":246308,"avatar_url":"https://avatars.githubusercontent.com/u/246308?v=3","gravatar_id":"","url":"https://api.github.com/users/himanshug","html_url":"https://github.com/himanshug","followers_url":"https://api.github.com/users/himanshug/followers","following_url":"https://api.github.com/users/himanshug/following{/other_user}","gists_url":"https://api.github.com/users/himanshug/gists{/gist_id}","starred_url":"https://api.github.com/users/himanshug/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/himanshug/subscriptions","organizations_url":"https://api.github.com/users/himanshug/orgs","repos_url":"https://api.github.com/users/himanshug/repos","events_url":"https://api.github.com/users/himanshug/events{/privacy}","received_events_url":"https://api.github.com/users/himanshug/received_events","type":"User","site_admin":false},"body":"personally speaking :) , I would have done `(int i = 1; i <= matcher.groupCount(); i++)`\r\nso that matcher.group(i) would be fine, easier to read that way.","created_at":"2015-11-07T06:11:46Z","updated_at":"2015-11-07T06:11:46Z","html_url":"https://github.com/metamx/java-util/pull/36#discussion_r44208619","pull_request_url":"https://api.github.com/repos/metamx/java-util/pulls/36","_links":{"self":{"href":"https://api.github.com/repos/metamx/java-util/pulls/comments/44208619"},"html":{"href":"https://github.com/metamx/java-util/pull/36#discussion_r44208619"},"pull_request":{"href":"https://api.github.com/repos/metamx/java-util/pulls/36"}}},"pull_request":{"url":"https://api.github.com/repos/metamx/java-util/pulls/36","id":50025414,"html_url":"https://github.com/metamx/java-util/pull/36","diff_url":"https://github.com/metamx/java-util/pull/36.diff","patch_url":"https://github.com/metamx/java-util/pull/36.patch","issue_url":"https://api.github.com/repos/metamx/java-util/issues/36","number":36,"state":"open","locked":false,"title":"Add javascript and regex parsers","user":{"login":"fjy","id":428325,"avatar_url":"https://avatars.githubusercontent.com/u/428325?v=3","gravatar_id":"","url":"https://api.github.com/users/fjy","html_url":"https://github.com/fjy","followers_url":"https://api.github.com/users/fjy/followers","following_url":"https://api.github.com/users/fjy/following{/other_user}","gists_url":"https://api.github.com/users/fjy/gists{/gist_id}","starred_url":"https://api.github.com/users/fjy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fjy/subscriptions","organizations_url":"https://api.github.com/users/fjy/orgs","repos_url":"https://api.github.com/users/fjy/repos","events_url":"https://api.github.com/users/fjy/events{/privacy}","received_events_url":"https://api.github.com/users/fjy/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-11-07T02:08:18Z","updated_at":"2015-11-07T06:11:46Z","closed_at":null,"merged_at":null,"merge_commit_sha":"898055d18da6193fcccd40a7191970c7a489f224","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/metamx/java-util/pulls/36/commits","review_comments_url":"https://api.github.com/repos/metamx/java-util/pulls/36/comments","review_comment_url":"https://api.github.com/repos/metamx/java-util/pulls/comments{/number}","comments_url":"https://api.github.com/repos/metamx/java-util/issues/36/comments","statuses_url":"https://api.github.com/repos/metamx/java-util/statuses/6f68c7dbec429aa4f1152c531e09399ecba1b9b5","head":{"label":"implydata:master","ref":"master","sha":"6f68c7dbec429aa4f1152c531e09399ecba1b9b5","user":{"login":"implydata","id":11660206,"avatar_url":"https://avatars.githubusercontent.com/u/11660206?v=3","gravatar_id":"","url":"https://api.github.com/users/implydata","html_url":"https://github.com/implydata","followers_url":"https://api.github.com/users/implydata/followers","following_url":"https://api.github.com/users/implydata/following{/other_user}","gists_url":"https://api.github.com/users/implydata/gists{/gist_id}","starred_url":"https://api.github.com/users/implydata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/implydata/subscriptions","organizations_url":"https://api.github.com/users/implydata/orgs","repos_url":"https://api.github.com/users/implydata/repos","events_url":"https://api.github.com/users/implydata/events{/privacy}","received_events_url":"https://api.github.com/users/implydata/received_events","type":"Organization","site_admin":false},"repo":{"id":45714160,"name":"java-util","full_name":"implydata/java-util","owner":{"login":"implydata","id":11660206,"avatar_url":"https://avatars.githubusercontent.com/u/11660206?v=3","gravatar_id":"","url":"https://api.github.com/users/implydata","html_url":"https://github.com/implydata","followers_url":"https://api.github.com/users/implydata/followers","following_url":"https://api.github.com/users/implydata/following{/other_user}","gists_url":"https://api.github.com/users/implydata/gists{/gist_id}","starred_url":"https://api.github.com/users/implydata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/implydata/subscriptions","organizations_url":"https://api.github.com/users/implydata/orgs","repos_url":"https://api.github.com/users/implydata/repos","events_url":"https://api.github.com/users/implydata/events{/privacy}","received_events_url":"https://api.github.com/users/implydata/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/implydata/java-util","description":"Utility code for java and jvm-based languages","fork":true,"url":"https://api.github.com/repos/implydata/java-util","forks_url":"https://api.github.com/repos/implydata/java-util/forks","keys_url":"https://api.github.com/repos/implydata/java-util/keys{/key_id}","collaborators_url":"https://api.github.com/repos/implydata/java-util/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/implydata/java-util/teams","hooks_url":"https://api.github.com/repos/implydata/java-util/hooks","issue_events_url":"https://api.github.com/repos/implydata/java-util/issues/events{/number}","events_url":"https://api.github.com/repos/implydata/java-util/events","assignees_url":"https://api.github.com/repos/implydata/java-util/assignees{/user}","branches_url":"https://api.github.com/repos/implydata/java-util/branches{/branch}","tags_url":"https://api.github.com/repos/implydata/java-util/tags","blobs_url":"https://api.github.com/repos/implydata/java-util/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/implydata/java-util/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/implydata/java-util/git/refs{/sha}","trees_url":"https://api.github.com/repos/implydata/java-util/git/trees{/sha}","statuses_url":"https://api.github.com/repos/implydata/java-util/statuses/{sha}","languages_url":"https://api.github.com/repos/implydata/java-util/languages","stargazers_url":"https://api.github.com/repos/implydata/java-util/stargazers","contributors_url":"https://api.github.com/repos/implydata/java-util/contributors","subscribers_url":"https://api.github.com/repos/implydata/java-util/subscribers","subscription_url":"https://api.github.com/repos/implydata/java-util/subscription","commits_url":"https://api.github.com/repos/implydata/java-util/commits{/sha}","git_commits_url":"https://api.github.com/repos/implydata/java-util/git/commits{/sha}","comments_url":"https://api.github.com/repos/implydata/java-util/comments{/number}","issue_comment_url":"https://api.github.com/repos/implydata/java-util/issues/comments{/number}","contents_url":"https://api.github.com/repos/implydata/java-util/contents/{+path}","compare_url":"https://api.github.com/repos/implydata/java-util/compare/{base}...{head}","merges_url":"https://api.github.com/repos/implydata/java-util/merges","archive_url":"https://api.github.com/repos/implydata/java-util/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/implydata/java-util/downloads","issues_url":"https://api.github.com/repos/implydata/java-util/issues{/number}","pulls_url":"https://api.github.com/repos/implydata/java-util/pulls{/number}","milestones_url":"https://api.github.com/repos/implydata/java-util/milestones{/number}","notifications_url":"https://api.github.com/repos/implydata/java-util/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/implydata/java-util/labels{/name}","releases_url":"https://api.github.com/repos/implydata/java-util/releases{/id}","created_at":"2015-11-06T23:40:01Z","updated_at":"2015-11-06T23:40:02Z","pushed_at":"2015-11-07T02:06:45Z","git_url":"git://github.com/implydata/java-util.git","ssh_url":"git@github.com:implydata/java-util.git","clone_url":"https://github.com/implydata/java-util.git","svn_url":"https://github.com/implydata/java-util","homepage":"","size":2614,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"metamx:master","ref":"master","sha":"08e083e51bda3f1575a97b39dc38f151da8ad562","user":{"login":"metamx","id":299329,"avatar_url":"https://avatars.githubusercontent.com/u/299329?v=3","gravatar_id":"","url":"https://api.github.com/users/metamx","html_url":"https://github.com/metamx","followers_url":"https://api.github.com/users/metamx/followers","following_url":"https://api.github.com/users/metamx/following{/other_user}","gists_url":"https://api.github.com/users/metamx/gists{/gist_id}","starred_url":"https://api.github.com/users/metamx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/metamx/subscriptions","organizations_url":"https://api.github.com/users/metamx/orgs","repos_url":"https://api.github.com/users/metamx/repos","events_url":"https://api.github.com/users/metamx/events{/privacy}","received_events_url":"https://api.github.com/users/metamx/received_events","type":"Organization","site_admin":false},"repo":{"id":2622329,"name":"java-util","full_name":"metamx/java-util","owner":{"login":"metamx","id":299329,"avatar_url":"https://avatars.githubusercontent.com/u/299329?v=3","gravatar_id":"","url":"https://api.github.com/users/metamx","html_url":"https://github.com/metamx","followers_url":"https://api.github.com/users/metamx/followers","following_url":"https://api.github.com/users/metamx/following{/other_user}","gists_url":"https://api.github.com/users/metamx/gists{/gist_id}","starred_url":"https://api.github.com/users/metamx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/metamx/subscriptions","organizations_url":"https://api.github.com/users/metamx/orgs","repos_url":"https://api.github.com/users/metamx/repos","events_url":"https://api.github.com/users/metamx/events{/privacy}","received_events_url":"https://api.github.com/users/metamx/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/metamx/java-util","description":"Utility code for java and jvm-based languages","fork":false,"url":"https://api.github.com/repos/metamx/java-util","forks_url":"https://api.github.com/repos/metamx/java-util/forks","keys_url":"https://api.github.com/repos/metamx/java-util/keys{/key_id}","collaborators_url":"https://api.github.com/repos/metamx/java-util/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/metamx/java-util/teams","hooks_url":"https://api.github.com/repos/metamx/java-util/hooks","issue_events_url":"https://api.github.com/repos/metamx/java-util/issues/events{/number}","events_url":"https://api.github.com/repos/metamx/java-util/events","assignees_url":"https://api.github.com/repos/metamx/java-util/assignees{/user}","branches_url":"https://api.github.com/repos/metamx/java-util/branches{/branch}","tags_url":"https://api.github.com/repos/metamx/java-util/tags","blobs_url":"https://api.github.com/repos/metamx/java-util/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/metamx/java-util/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/metamx/java-util/git/refs{/sha}","trees_url":"https://api.github.com/repos/metamx/java-util/git/trees{/sha}","statuses_url":"https://api.github.com/repos/metamx/java-util/statuses/{sha}","languages_url":"https://api.github.com/repos/metamx/java-util/languages","stargazers_url":"https://api.github.com/repos/metamx/java-util/stargazers","contributors_url":"https://api.github.com/repos/metamx/java-util/contributors","subscribers_url":"https://api.github.com/repos/metamx/java-util/subscribers","subscription_url":"https://api.github.com/repos/metamx/java-util/subscription","commits_url":"https://api.github.com/repos/metamx/java-util/commits{/sha}","git_commits_url":"https://api.github.com/repos/metamx/java-util/git/commits{/sha}","comments_url":"https://api.github.com/repos/metamx/java-util/comments{/number}","issue_comment_url":"https://api.github.com/repos/metamx/java-util/issues/comments{/number}","contents_url":"https://api.github.com/repos/metamx/java-util/contents/{+path}","compare_url":"https://api.github.com/repos/metamx/java-util/compare/{base}...{head}","merges_url":"https://api.github.com/repos/metamx/java-util/merges","archive_url":"https://api.github.com/repos/metamx/java-util/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/metamx/java-util/downloads","issues_url":"https://api.github.com/repos/metamx/java-util/issues{/number}","pulls_url":"https://api.github.com/repos/metamx/java-util/pulls{/number}","milestones_url":"https://api.github.com/repos/metamx/java-util/milestones{/number}","notifications_url":"https://api.github.com/repos/metamx/java-util/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/metamx/java-util/labels{/name}","releases_url":"https://api.github.com/repos/metamx/java-util/releases{/id}","created_at":"2011-10-21T18:41:10Z","updated_at":"2015-08-25T21:04:31Z","pushed_at":"2015-11-07T02:40:45Z","git_url":"git://github.com/metamx/java-util.git","ssh_url":"git@github.com:metamx/java-util.git","clone_url":"https://github.com/metamx/java-util.git","svn_url":"https://github.com/metamx/java-util","homepage":"","size":2614,"stargazers_count":15,"watchers_count":15,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":27,"mirror_url":null,"open_issues_count":4,"forks":27,"open_issues":4,"watchers":15,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/metamx/java-util/pulls/36"},"html":{"href":"https://github.com/metamx/java-util/pull/36"},"issue":{"href":"https://api.github.com/repos/metamx/java-util/issues/36"},"comments":{"href":"https://api.github.com/repos/metamx/java-util/issues/36/comments"},"review_comments":{"href":"https://api.github.com/repos/metamx/java-util/pulls/36/comments"},"review_comment":{"href":"https://api.github.com/repos/metamx/java-util/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/metamx/java-util/pulls/36/commits"},"statuses":{"href":"https://api.github.com/repos/metamx/java-util/statuses/6f68c7dbec429aa4f1152c531e09399ecba1b9b5"}}}}