去除文章内容的手机号码
package cn.com.test;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
public class replacePhone {
?public static void main(String[] args) {
??// 手机号码判断
??String htmlContent = "abcdefghigklmnopqrstuvwxyz13666666666adsfhalksabcdefghigklmnopqrstuvwxyzdddddda13666666666";
??String patternStrs = "13\\d{9}";
??replaceMobileTelephone(patternStrs, htmlContent);
?}
?public static String replaceMobileTelephone(String rule, String content) {
??try {
???PatternCompiler complier = new Perl5Compiler();
???PatternMatcher matcher = new Perl5Matcher();
???Pattern patternForLink = complier.compile(rule, Perl5Compiler.CASE_INSENSITIVE_MASK);
???PatternMatcherInput input = new PatternMatcherInput(content);
???while (matcher.contains(input, patternForLink)) {
????content = content.replace(matcher.getMatch().toString(), "***");
???}
??} catch (Exception e) {
???e.printStackTrace();
??}
??return content;
?}
}
下面是用到的jar包。
?
真是晕呀!java有自带的呀!
弄了半小时弄出来了,在看replaceAll这个函数,也不知道自己在弄什么呀!
哈哈。。。
?
代码如下:“文档内容”.replaceAll("正则表达式","要替换成什么");