JAVA 获取 URL 指定参数的值

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) + "\t\t";
    }
    return result;
}

 

posted @ 2021-09-01 10:02  VipSoft  阅读(2357)  评论(0编辑  收藏  举报