正则表达式如何匹配Url编码后的字符串
1个回答
展开全部
// url正则
为了避免与字符串转义,要将一个\写成\\,如\d写成\\d
如"[,\\s]+"匹配"a,b c"
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegularExpressionTest {
public static void main(String[] args) {
Pattern p = Pattern.compile("^(http|www|ftp|)?(://)?(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*((:\\d+)?)(/(\\w+(-\\w+)*))*(\\.?(\\w)*)(\\?)?(((\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*(\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*)*(\\w*)*)$",Pattern.CASE_INSENSITIVE );
Matcher m = p.matcher("http://www.qqgb.com/Program/Java/JavaFAQ/JavaJ2SE/Program_146959.html");
if(m.find()){
System.out.println(m.group());
}
m = p.matcher("http://baike.baidu.com/view/230199.htm?fr=ala0_1");
if(m.find()){
System.out.println(m.group());
}
m = p.matcher("http://www.google.cn/gwt/x?u=http%3A%2F%2Fanotherbug.blog.chinajavaworld.com%2Fentry%2F4550%2F0%2F&btnGo=Go&source=wax&ie=UTF-8&oe=UTF-8");
if(m.find()){
System.out.println(m.group());
}
m = p.matcher("http://zh.wikipedia.org:80/wiki/Special:Search?search=tielu&go=Go");
if(m.find()){
System.out.println(m.group());
}
}
}
为了避免与字符串转义,要将一个\写成\\,如\d写成\\d
如"[,\\s]+"匹配"a,b c"
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegularExpressionTest {
public static void main(String[] args) {
Pattern p = Pattern.compile("^(http|www|ftp|)?(://)?(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*((:\\d+)?)(/(\\w+(-\\w+)*))*(\\.?(\\w)*)(\\?)?(((\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*(\\w*%)*(\\w*\\?)*(\\w*:)*(\\w*\\+)*(\\w*\\.)*(\\w*&)*(\\w*-)*(\\w*=)*)*(\\w*)*)$",Pattern.CASE_INSENSITIVE );
Matcher m = p.matcher("http://www.qqgb.com/Program/Java/JavaFAQ/JavaJ2SE/Program_146959.html");
if(m.find()){
System.out.println(m.group());
}
m = p.matcher("http://baike.baidu.com/view/230199.htm?fr=ala0_1");
if(m.find()){
System.out.println(m.group());
}
m = p.matcher("http://www.google.cn/gwt/x?u=http%3A%2F%2Fanotherbug.blog.chinajavaworld.com%2Fentry%2F4550%2F0%2F&btnGo=Go&source=wax&ie=UTF-8&oe=UTF-8");
if(m.find()){
System.out.println(m.group());
}
m = p.matcher("http://zh.wikipedia.org:80/wiki/Special:Search?search=tielu&go=Go");
if(m.find()){
System.out.println(m.group());
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询