zoukankan      html  css  js  c++  java
  • 从一段文字中提取出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.close(); w.flush(); w.close(); System.out.println("执行完毕"); } }
  • 相关阅读:
    菜鸟学IT之豆瓣爬取初体验
    菜鸟学IT之python网页爬取多页爬取
    菜鸟学IT之python网页爬取初体验
    菜鸟学IT之python词云初体验
    菜鸟学IT之python3关于列表,元组,字典,集合浅认识!
    整合ssm框架
    MyBatis
    服务出现服务名无效的原因及解决方法
    数据库(三)
    数据库(二)
  • 原文地址:https://www.cnblogs.com/yanghaolie/p/6418779.html
Copyright © 2011-2022 走看看