JAVA 获取 URL 指定参数的值
@Test void regexTest() { String url = "https://www.cnblogs.com/vipsoft/p/15213397.html?name=abc&age=123"; String result = getFieldValue(url, "name"); logger.info(result); }
/** * 获取字段值 * * @param urlStr * @param field * @return */ private static String getFieldValue(String urlStr, String field) { String result = ""; Pattern pXM = Pattern.compile(field + "=([^&]*)"); Matcher mXM = pXM.matcher(urlStr); while (mXM.find()) { result += mXM.group(1) + " "; } return result; }