日期:2014-05-17 浏览次数:20840 次
package regExp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UrlEncoder {
public static String getParameter(String url,String param){
Pattern p=Pattern.compile("(?<=[\\?&]"+param+"=)\\w+");
Matcher m = p.matcher(url);
if(m.find()){
return m.group(0);
}
return null;
}
public static String encode(String url){
String str = url.substring(0, url.lastIndexOf("/"));
String ac = getParameter(url,"ac");
String op = getParameter(url,"op");
return str+"/" + ac + (op == null ? "" : "/" + op );
}
public static void main(String[] args) {
String url = "http://www.xxx.com/web/cp.jsp?ac=login&op=checkemail";
// String url = "http://www.xxx.com/web/cp.jsp?ac=login1";
System.out.println(encode(url));
}
}