符合一定规则的表达式
用于操作字符串
特点:用一些特殊的符号来表达代码操作
好处:可以简化对字符串的复杂操作
matches();用规则匹配字符串
正则表达式对字符串切割
当你需要对一个结果进行重用的话,你把他封装成组
语法:(.)\\1 前面表示组 后面表示重用第一组
(.)\\1+ 则表示叠词出现成多次
替换 (.\\1) (获得规则中的第一个组 $1) 相当于去掉字符串中的重写字母
获取(取出符合规则的子串)
将正则表达式封装成对象
让正则表达式对象和操作的字符相关联
关联后,获取一个叫做正则匹配引擎
通过引擎对符合规则子串进行操作
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexDemo {
public static void main(String[] args) {
// 检验QQ 5-10
// method();
// methodRegex();
// checkTel();
// splitStr();
// replaceAllStr();
// repeat();
// checkEmail();
// get();
// getMatcher();
getIP();
}
/**
* 匹配QQ的合法性,不实用正则表达式
*/
public static void method() {
String qq = "10021245";
if (qq.length() > 5 && qq.length() < 11) {
if (qq.startsWith("0")) {
System.out.println("start no 0");
} else {
char[] ar = qq.toCharArray();
boolean flag = false;
for (int i = 0; i < ar.length; i++) {
if (!(ar[i] >= '0' && ar[i] <= '9')) {
flag = true;
break;
}
}
if (flag) {
System.out.println("no qq");
} else {
System.out.println("yes");
}
}
} else {
System.out.println("length error");
}
}
/**
* 正则表达式检测qq合法性
*/
public static void methodRegex() {
String qq = "12121212";
String regex = "[1-9][0-9]{4,14}";
boolean flag = qq.matches(regex);
System.out.println(flag);
}
/**
* 正则表达式匹配电话号码合法性
*/
public static void checkTel() {
String tel = "13971518451";
String regex = "1[358]\\d{9}";
System.out.println(tel.matches(regex));
}
/**
* 按照规则分割字符串
*/
public static void splitStr() {
String s1 = "zhangsan,lisi,wangwu";
String r1 = ",";
String[] arr1 = s1.split(r1);
for (String i : arr1) {
System.out.println(i);
}
String s2 = "zhangsan wangwu lisi";
String r2 = " +";
// + 表示一次或者多次
String[] arr2 = s2.split(r2);
for (String i : arr2) {
System.out.println(i);
}
}
/**
* 正则表达式替换指定字符
*/
public static void replaceAllStr() {
String str = "adadeawerw13971518451ishndksndfmfobsienmsoapmzkiemwamiwuancmkallaopq";
String regex = "\\d{5,}";// 替换数字连续超过五个以上的字符
str = str.replaceAll(regex, "*");
System.out.println(str);
}
/**
* 正则表达式替换重复字符
*/
public static void repeat() {
String str = "aabbcceeddffgg";
String regex = "(.)\\1";// (.)任意字符封装成组 \\1则表示引用第一个组
System.out.println(str.replaceAll(regex, "$1")); // $1获取组中第一个规则
}
/**
* 正则表达式匹配邮箱
*/
public static void checkEmail() {
String email = "afterxiong@sin.cn";
String regex = "\\w+@\\w+(\\.\\w+)+";
System.out.println(email.matches(regex));
}
/**
* 正则表达式取出指定字符
*/
public static void get() {
String str = "ming tian jiu yao fang jia le";// 取出三个字符的拼音
String regex = "\\b[a-z]{3}\\b";// \\b单词边界
// 将正则表达式封装成对象
Pattern p = Pattern.compile(regex);
// 将正则表达式和操作的字符串关联,获取匹配器对象
Matcher matcher = p.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
System.out.println(matcher.start() + " " + matcher.end());
}
}
/**
* @category String
* @param 我要
* ...我我我我...要要要...学学学学学...编编编编编编编编...程编
* @param 我要学编程
*/
public static void getMatcher() {
String str = "我...我我我我...要要要...学学学学学...编编编编编编编编...程编";
str = str.replaceAll("\\.", "");
str = str.replaceAll("(.)\\1", "");
System.out.println(str);
}
/**
* @param 192.68.1.254 102.168.2.6 10.10.0.1 192.168.0.1 172.168.11.51
* 2.2.2.2 IP地址排序 1、按照每一段至少需要最多的0进行补齐,至多两个0 2、将每一段保留3位
* @param IP地址排序
*/
public static void getIP() {
String ip = "192.68.1.254 102.168.2.6 10.10.0.1 192.168.0.1 172.168.11.51 2.2.2.2";
ip = ip.replaceAll("(\\d+)", "00$1");//-----(\\d+)组中的数字出现多次,使用前面的组,00$1---并且在组的前面加连个0
ip = ip.replaceAll("0*(\\d{3})", "$1");//---0*(\\d{3}) 0出现零次或者多次,数字出现三次,$1---引起前面的组
String[] arr=ip.split(" ");
TreeSet<String> tree=new TreeSet<String>();
for(String i:arr){
tree.add(i);
}
for(String t:tree){
t=t.replaceAll("0*(\\d+)", "$1");//----0*(\\d+) 0出现零次或者多次 数字出现一次或者多次 $1---使用前面的组
System.out.println(t);
}
}
}