Java截取url参数的值
1 public static void main(String[] args){ 2 String Url = "http://localhost:8180/guozhi/login.jsp?preappid=guozhi&preownerid=1234"; 3 4 System.out.println("参数preappid的值是:"+getUrl(Url,"preappid")); 5 System.out.println("参数preownerid的值是:"+getUrl(Url,"preownerid")); 6 } 7 public static String getUrl(String url, String name){ 8 url +="&"; 9 String pattern = "(\\?|&){1}#{0,1}" + name + "=[a-zA-Z0-9]*(&{1})"; 10 Pattern r = Pattern.compile(pattern); 11 Matcher matcher = r.matcher(url); 12 if(matcher.find()){ 13 // System.out.println(matcher.group(0)); 14 return matcher.group(0).split("=")[1].replace("&",""); 15 }else{ 16 return null; 17 } 18 }