package stringTest;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class regTest {
public static void main(String[] args) {
String res = null;
// res = extTitle();
// res=extImg();
// res = extSite();
res = recoverUrl();
System.out.println(res);
}
private static String recoverUrl() {
String str = "../../../tplimg/dailyindexbiaoshi_01.gif";
String htmlurl = "Http://www.legaldaily.com.cn/0801/2009-06/11/content_1104248.htm";
String res = null;
int lev = 0;
String tmp = htmlurl;
if (str.indexOf("../") == 0) {
lev = str.lastIndexOf("../") / 3 + 1;
System.out.println(lev);
for (int i = 0; i <= lev; i++) {
tmp = tmp.substring(0, tmp.lastIndexOf("/"));
System.out.println(tmp);
}
}
res = tmp + "/" + str.substring(3 * lev);
return res;
}
private static String extSite() {
String res = null;
String str = "Http://news.china.com/zh_cn/social/1007/20090629/15539893.html";
if (str.indexOf("//") != -1) {
String tmp = str.substring(str.indexOf("//") + 2);
res = str.substring(0, str.indexOf("//") + 2 + tmp.indexOf("/"));
}
return res;
}
private static String extImg() {
String res = "";
// getHtmlCode ghc =new getHtmlCode();
String str = null;
try {
str = getHtmlCode
.getHtmlCodeByUrl(new URL(
"http://news.dayoo.com/ent/news/2008-04/03/content_3351975.htm"));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// String str="32324dfdf d<img
// src=/"http://images.dayoo.com/include/img/attachement/jpg/site1/20090330/00b0d03f3fa20b3ab81604//.jpg/"
// border /"gfdfe3";
String regex = "<img src=/"([//w//W]+?)/"";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
int gc = matcher.groupCount();
for (int i = 0; i <= gc; i++)
// System.out.println("group " + i + " :" + matcher.group(i));
res = matcher.group(1);
System.out.println(res);
}
return res;
}
private static String extTitle() {
String res = null;
String str = "<fddfdf> <title>幸福的感觉—我和老公的故事 -美文故事-散文随笔- 文章阅读网</title> <metdfe";
String regex = "<title>([//w//W]*)</title>";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
int gc = matcher.groupCount();
for (int i = 0; i <= gc; i++)
// System.out.println("group " + i + " :" + matcher.group(i));
res = matcher.group(1);
}
return res;
}
}