从一段文字中提取出uri信息

package handle.groupby;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.mockito.asm.tree.IntInsnNode;

public class GetUrlFromString {

    @SuppressWarnings("resource")
    public static void main(String[] args) throws IOException {
        String line="";
        Pattern pattern = Pattern.compile("([\\w.]{1}[\\w\\/.\\s]*[\\w]{1})",Pattern.CASE_INSENSITIVE);
        
        BufferedReader r= new BufferedReader(new FileReader(args[0]));
        BufferedWriter w=new BufferedWriter(new FileWriter(args[1])) ;
        while ((line=r.readLine())!=null) {
            String source = line;
             Matcher matcher = pattern.matcher(source);
                while(matcher.find()){
//                    System.out.println(matcher.group(matcher.groupCount()));
                    String url=matcher.group(matcher.groupCount());
                    if (url.contains(".")) {
                        String resUrl="";
                        String resUrl2="";
                        if (url.contains("/")) {
                //这个判断是为了提取出短域名的网站级访问访问信息,不需要可以删掉。
                //例如从:汉字汉字汉字t.cn/RVIIIj8汉字汉字 中提取出 t.cn/RVIIIj8而不是t.cn
int i =url.lastIndexOf("/"); int i2 =url.indexOf("/"); if (i==i2) { resUrl=url; }else { resUrl =url.split("/")[0]; } }else { resUrl=url; } //去空格 resUrl= resUrl.replaceAll(" ", ""); w.write(source+"|"+resUrl); w.write("\r\n"); } } } r.close(); w.flush(); w.close(); System.out.println("执行完毕"); } }

 

转载于:https://www.cnblogs.com/yanghaolie/p/6418779.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值