import java
.util
.regex
.Matcher
;
import java
.util
.regex
.Pattern
;
public class RegexFunctionDemo {
public static void main(String
[] args
) {
functionDemo_1();
functionDemo_2();
functionDemo_3();
functionDemo_4();
}
private static void functionDemo_4() {
String str
= "da jia shu yi la,ming tian fang jia le!";
String regex
= "\\b[a-zA-Z]{3}\\b";
Pattern p
= Pattern
.compile(regex
);
Matcher m
= p
.matcher(str
);
while(m
.find()) {
System
.out
.println(m
.group());
System
.out
.println(str
.substring(m
.start(),m
.end()));
}
}
private static void functionDemo_3() {
String str
= "18991538621";
str
= str
.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
System
.out
.println(str
);
}
private static void functionDemo_2() {
String str
= "a;lkghal;kkkkjfeoij###";
String regex
= "(.)\\1+";
String
[] strs
= str
.split(regex
);
for (String s
:strs
) {
System
.out
.println(s
);
}
}
private static void functionDemo_1() {
String str
= "18991538621";
String regex
= "1[358]\\d{9}";
boolean b
= str
.matches(regex
);
System
.out
.println(str
+":"+b
);
}
}
转载请注明原文地址: https://mac.8miu.com/read-503632.html