/** * Gets the set of unique path parameters used in the given URI. If a parameter is used twice * in the URI, it will only show up once in the set. */ static Set<String> parsePathParameters(String path) { Matcher m = PARAM_URL_REGEX.matcher(path); Set<String> patterns = new LinkedHashSet<>(); while (m.find()) { patterns.add(m.group(1)); } return patterns; }
static boolean isJava8BelowUpdate45(String jvmVersion) { Matcher matcher = JAVA_8_RELEASE_VERSION_SCHEME.matcher(jvmVersion); if (matcher.matches()) { int update = Integer.parseInt(matcher.group(1)); return update < 45; } matcher = JAVA_8_DEV_VERSION_SCHEME.matcher(jvmVersion); if (matcher.matches()) { int update = Integer.parseInt(matcher.group(1)); return update < 45; } matcher = Pattern.compile("1\\.8\\.0-b\\d+").matcher(jvmVersion); return matcher.matches(); }
private void assertDescriptionContainsExpectedPath(ClassPathResource resource, String expectedPath) { Matcher matcher = DESCRIPTION_PATTERN.matcher(resource.getDescription()); assertTrue(matcher.matches()); assertEquals(1, matcher.groupCount()); String match = matcher.group(1); assertEquals(expectedPath, match); }
String time = "14:35:59.99"; String timeRegex = "([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\\.([0-9]{1,3}))?"; Pattern pattern = Pattern.compile(timeRegex); Matcher matcher = pattern.matcher(time); if (matcher.matches()) { String hours = matcher.group(1); String minutes = matcher.group(2); String seconds = matcher.group(3); String miliSeconds = matcher.group(4); System.out.println(hours + ", " + minutes + ", " + seconds + ", " + miliSeconds); }
/** * Extracts the hostname from a "host:port" address string. * @param address address string to parse * @return hostname or null if the given address is incorrect */ public static String getHost(String address) { Matcher matcher = HOST_PORT_PATTERN.matcher(address); return matcher.matches() ? matcher.group(1) : null; }
private static String resolveJarUrl(Class<?> clazz) { URL location = clazz.getResource('/' + clazz.getName().replace('.', '/') + ".class"); if (location != null) { Matcher matcher = Pattern.compile("(jar:file.*-[\\d.]+(-rc[\\d]+|-SNAPSHOT)?.jar)!.*$").matcher(location.toString()); if (matcher.matches()) { return matcher.group(1); } } return null; } }
/** * Extracts the port number from a "host:port" address string. * @param address address string to parse * @return port number or null if the given address is incorrect */ public static Integer getPort(String address) { Matcher matcher = HOST_PORT_PATTERN.matcher(address); return matcher.matches() ? Integer.parseInt(matcher.group(2)) : null; }
public static String parseRevisionFromSvnInfo(String svnInfo) { String s = svnInfo.replaceAll("\\s", " "); Pattern pattern = Pattern.compile(".*revision=\"(\\d+)\".*"); Matcher matcher = pattern.matcher(s); if (matcher.matches()) { return matcher.group(1); } throw bomb("Can not parse revision from svninfo: \n" + svnInfo); }
/** * parse key-value pair. * * @param str string. * @param itemSeparator item separator. * @return key-value map; */ private static Map<String, String> parseKeyValuePair(String str, String itemSeparator) { String[] tmp = str.split(itemSeparator); Map<String, String> map = new HashMap<String, String>(tmp.length); for (int i = 0; i < tmp.length; i++) { Matcher matcher = KVP_PATTERN.matcher(tmp[i]); if (!matcher.matches()) { continue; } map.put(matcher.group(1), matcher.group(2)); } return map; }
/** * Checks to see if the supplied {@code String} has any placeholders * that are not specified as constants on this class and throws an * {@code IllegalArgumentException} if so. */ private void checkForInvalidPlaceholders(String message) throws IllegalArgumentException { Matcher matcher = PATTERN.matcher(message); while (matcher.find()) { String match = matcher.group(); if (!ALLOWED_PLACEHOLDERS.contains(match)) { throw new IllegalArgumentException("Placeholder [" + match + "] is not valid"); } } }
private void handleAppsDelete(HttpExchange httpExchange) throws IOException { EurekaHttpResponse<?> httpResponse; String path = httpExchange.getRequestURI().getPath(); Matcher matcher; if ((matcher = Pattern.compile("/v2/apps/([^/]+)/([^/]+)").matcher(path)).matches()) { httpResponse = requestHandler.cancel(matcher.group(1), matcher.group(2)); } else if ((matcher = Pattern.compile("/v2/apps/([^/]+)/([^/]+)/status").matcher(path)).matches()) { httpResponse = requestHandler.deleteStatusOverride(matcher.group(1), matcher.group(2), null); } else { httpExchange.sendResponseHeaders(HttpServletResponse.SC_NOT_FOUND, 0); return; } mapResponse(httpExchange, httpResponse); }
/** * parse key-value pair. * * @param str string. * @param itemSeparator item separator. * @return key-value map; */ private static Map<String, String> parseKeyValuePair(String str, String itemSeparator) { String[] tmp = str.split(itemSeparator); Map<String, String> map = new HashMap<String, String>(tmp.length); for (int i = 0; i < tmp.length; i++) { Matcher matcher = KVP_PATTERN.matcher(tmp[i]); if (!matcher.matches()) { continue; } map.put(matcher.group(1), matcher.group(2)); } return map; }