@Test public void findsMultipleGroups() { Pattern pattern = compile("\\w* "); Matcher matcher = pattern.matcher("this is a sentance"); log.info("GroupCount: " + matcher.groupCount()); while (matcher.find()) { log.info("Found group : [" + matcher.group().trim() + "]"); } }
@Test public void findStructuredGroups() { Pattern pattern = compile("\\{\\w*\\}"); Matcher matcher = pattern.matcher("/{parameter1}/some/path/{parameter2}/end"); while (matcher.find()) { log.info("Found parameter: [" + matcher.group().replaceAll("\\{|\\}", "") + "]"); } String output = matcher.replaceAll("(.*)"); log.info("Replaced output: [" + output + "]"); }
@Test public void findStructuredGroupsWithParenthesis() { Pattern pattern = compile("\\([^\\)]*\\)"); Matcher matcher = pattern.matcher("Some sentance (parameter 1d) /some/path/ (parameter 2 ) /end"); while (matcher.find()) { log.info("Found parameter: [" + matcher.group().replaceAll("\\(|\\)", "") + "]"); } String output = matcher.replaceAll("(.*)"); log.info("Replaced output: [" + output + "]"); }
public ControllerRequest(Class<? extends Controller> controllerClass, Matcher matchedUrl) { this.controllerClass = controllerClass; if (matchedUrl.groupCount() > 0) { List<String> argsList = new ArrayList<String>(matchedUrl.groupCount()); for (int i = 1; i <= matchedUrl.groupCount(); i++) { argsList.add(matchedUrl.group(i)); } this.args = argsList; } else { this.args = new ArrayList<String>(0); } }