/*
* week_02_作业二
* @detail:完成clean函数,其功能为:屏蔽字符串参数text中的网页链接信息,并按照返回屏蔽后的结果
* @返回格式:对第1-n个链接替换为OMIT1-OMITn,返回替换后的字符串
* @author: vanxin
* @email: vanxinliu@gmail.com
* @version: 2.0
* @改进了匹配的正则表达式,使之对域名为0个字符的放行,如www..com,http://.com
* @completed time: 2013/10/7
*/
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UnLinker
{
/*
* @定义匹配的正则表达式格式
* @前缀为:http://, http://www., www. 三种情况中的一种
* @域名包括一个或者多个字符,每个字符是一个英文字母(大小写均可),或者一个数字(0-9),或者一个句点(.)
* @后缀为.com, .org, .edu, .info, .tv五种中德一种
*/
static final String REGEX_HTTP = "(http://www.|http://|www.)([A-Za-z0-9.]+)(.com|.org|.edu|.info|.tv)";
/*
* 定义用来替换的字符串
*/
static final String Replace_String = "OMIT";
/*
* 主要匹配替换函数
*/
String clean(String text)
{
//计数器
int count = 1;
//结果返回字符串
String Answer;
//编译完成所需格式
Pattern p = Pattern.compile(REGEX_HTTP);
Matcher m = p.matcher(text);
//按照语法格式需要定义缓冲区
StringBuffer sb = new StringBuffer();
//对字符串进行扫描,将符合格式的替换
while(m.find()){
m.appendReplacement(sb,Replace_String+count);
count++;
}
m.appendTail(sb);
//结果转换
Answer = sb.toString();
return Answer;
}
/*
* 主函数
*/
public static void main(String[] args) throws IOException
{
Scanner in = new Scanner(new File("UnLinker.in"));
String text;
UnLinker ul = new UnLinker();
while (in.hasNextLine())
{
text = in.nextLine();
System.out.println(ul.clean(text));
}
in.close();
}
}
* week_02_作业二
* @detail:完成clean函数,其功能为:屏蔽字符串参数text中的网页链接信息,并按照返回屏蔽后的结果
* @返回格式:对第1-n个链接替换为OMIT1-OMITn,返回替换后的字符串
* @author: vanxin
* @email: vanxinliu@gmail.com
* @version: 2.0
* @改进了匹配的正则表达式,使之对域名为0个字符的放行,如www..com,http://.com
* @completed time: 2013/10/7
*/
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UnLinker
{
/*
* @定义匹配的正则表达式格式
* @前缀为:http://, http://www., www. 三种情况中的一种
* @域名包括一个或者多个字符,每个字符是一个英文字母(大小写均可),或者一个数字(0-9),或者一个句点(.)
* @后缀为.com, .org, .edu, .info, .tv五种中德一种
*/
static final String REGEX_HTTP = "(http://www.|http://|www.)([A-Za-z0-9.]+)(.com|.org|.edu|.info|.tv)";
/*
* 定义用来替换的字符串
*/
static final String Replace_String = "OMIT";
/*
* 主要匹配替换函数
*/
String clean(String text)
{
//计数器
int count = 1;
//结果返回字符串
String Answer;
//编译完成所需格式
Pattern p = Pattern.compile(REGEX_HTTP);
Matcher m = p.matcher(text);
//按照语法格式需要定义缓冲区
StringBuffer sb = new StringBuffer();
//对字符串进行扫描,将符合格式的替换
while(m.find()){
m.appendReplacement(sb,Replace_String+count);
count++;
}
m.appendTail(sb);
//结果转换
Answer = sb.toString();
return Answer;
}
/*
* 主函数
*/
public static void main(String[] args) throws IOException
{
Scanner in = new Scanner(new File("UnLinker.in"));
String text;
UnLinker ul = new UnLinker();
while (in.hasNextLine())
{
text = in.nextLine();
System.out.println(ul.clean(text));
}
in.close();
}
}