正则测试 java源码

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;
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值