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

时间:2023-03-10 04:28:38
从一段文字中提取出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("执行完毕");
} }