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("正则表达式","要替换成什么");
本文提供了一个使用Java实现的简单示例,演示如何利用正则表达式匹配并替换字符串中的手机号码。通过Perl5Compiler和Perl5Matcher完成模式编译与匹配过程,最终将所有匹配到的手机号码替换为指定的字符。
2259

被折叠的 条评论
为什么被折叠?



