package regular;
public class RegexDemo1 {
/**
* @正则表达式的具体操作功能:
* 特点:用一些特定的符号表示一些代码操作,这样就简化方法
*
* 1.匹配:String matches方法。用规则匹配整个字符串,只要有一处不符合规则就匹配结束,返回false;
*
* 2.切割:String split(String regex)
*
* 3.替换:String replaceAll()
*
*
*/
public static void main(String[] args) {
//demo();
//sop('a'+0);
//check_qq();
//phone();
//split_demo("zhangsan lisi wangwu "," +");//按多个空格切割
//split_demo("zhangsan.lisi.wangwu","\\.");//按.切割
//split_demo("c:\\abc\\a.txt","\\\\");
//split_demo("agsfkkdjaahbeqqazzzzzzzzzzzzrfs","(.)\\1+");//按照叠词完成切割
String str = "wef152346578954ty123456485fsaijger154354ffafd";//将数字替换成#号
//replaceAllDemo(str,"\\d{5,}","#");
String str1="agvsdkkddddddasfzzdazzzzzzzfewffrfas";
//replaceAllDemo(str1,"(.)\\1+","&");//将叠词替换成&agvsd&&asf&da&few&rfas
//replaceAllDemo(str1,"$1");//将叠词替换成单个字符agvsdkdasfzdazfewfrfas,用'$'返回组的内容
replaceAllDemo(str1,"$1");//
}
public static void replaceAllDemo(String str,String reg,String newStr){
str = str.replaceAll(reg,newStr);
sop(str);
}
private static void split_demo(String str,String reg) {
String[] arr = str.split(reg);
//高级for循环,遍历数组
for(String s : arr){
sop(s);
}
}
private static void check_qq() {
String qq = "123456";
String reg = "[1-9]\\d{4,11}";
boolean flag = qq.matches(reg);
if(flag)
sop("qq : " +qq);
else
sop("输入错误");
}
//13xxx,15xxx,18xxx
private static void phone() {
String tel = "123456";
String reg = "1[358]\\d{9}";
boolean flag = tel.matches(reg);
if(flag)
sop("tel : " +tel);
else
sop("输入错误");
}
private static void demo() {
String str = "b345";
String reg ="\\w\\d*\\w";
boolean b = str.matches(reg);
System.out.println(b);
}
public static void sop(Object obj){
System.out.println(obj);
}
}
原文链接:https://www.f2er.com/regex/360501.html