正则表达式(1)

package re;

import java.util.Arrays;

/**
 * @Author: layman
 * @Date:Create:in 2021/4/30 9:03
 * @Description: 正则表达式的基本使用:
 * [a] a 都表示a
 * [ab] a或者b
 * [a-z] 所有的小写字母
 * [a-zA-Z0-9_] 数字字母下划线
 * [^a] 非字母a
 * [^ab] 非a非b  WARN:^在[]内部表示非,否则表示字符开头
 * d 表示数字 等价于[0-9] (digital)
 * D 表示非数字 等价于[^0-9]
 * w 表示单词字符 数字字母下划线 等价于[a-zA-Z0-9_] (word)
 * W 表示非单词字符 [^a-zA-Z0-9_]
 * s 表示空白字符 space 	 
 
 * S 表示非空白字符
 * . 表示任意字符 非 
 

 * . 表示真正的.
 * ^ 表示字符串的开头
 * $ 表示字符串的结尾
 * 数量词:
 * a{m} 表示m个a
 * a{m,} 表示至少m个a
 * a{m,n} 表示至少m个a,至多n个a [m,n]
 * a+ 表示至少一个a 等价于a{1,}
 * a* 表示至少0个 a{0,}
 * a? 表示0个或者一个a 等价于a{0,1}
 * () 捕获组
 * 1 取第一组
 * 正则中的两种情况:
 * 贪婪式(默认):常用
 * 懒惰式:在数量词的后面添加?表示启用懒惰式
 */
public class RegularExpressionDemo_01 {
    public static void main(String[] args) {
//        String s = "12345";
//        boolean result = s.matches("\d*");
//        验证手机号
//        String phone = "18827262111";
//        boolean result = phone.matches("1[345678]\d{9}");
//        String email = "biglayman@qq.com";
//        boolean result = email.matches("\w{3,15}@\S+\.(com|cn|edu|org)");
//        String s = "uisauodh123132131houigb4uo12h4ouih1341hio24o1";
//        String result = s.replaceAll("\d", "");
//        String s = "uisauodh123132131houigb4uo12h4ouih1341hio24o1";
//        String result = s.replaceAll("\d+", "@");
//        String s = "uisauodh123132131houigb4uo12h4ouih1341hio24o1";
//        String result = s.replaceAll("\d+?", "@");
//        String s = "我我我今今今今天天天不不不上上班";
//        String result = s.replaceAll("(.)\1+", "$1");
//        System.out.println(result);
//        脱敏处理
//        String s = "18827262111";
//        System.out.println(s.replaceAll("(\d{3})(\d{4})(\d{3})", "$1****$3"));
//        String s = "192.168.1.10";
//        String[] words = s.split("\.");
//        System.out.println(Arrays.toString(words));
//        String s = "..192.168.1..10";
//        String[] words = s.split("\.");
//        System.out.println(Arrays.toString(words));//[, , 192, 168, 1, , 10]
        String s = "..192.168.1..10......";
        String[] words = s.split("\.");
        System.out.println(Arrays.toString(words));//[, , 192, 168, 1, , 10]
    }
}
原文地址:https://www.cnblogs.com/shun998/p/14721050.html