java——正则表达式

1.字符集 [ ] 一个字符位置上出现字符的选择范围

[abc] abc中的任意一个字符
[^abc] 除了abc中的任意一个字符
[a-z] a-z中的任意一个字符
[a-z A-Z 0-9]

String regex = "[abc]";//abc中其中的一个字符
        System.out.println("a".matches(regex));//true
        System.out.println("ab".matches(regex));//false出现了两个字符
        System.out.println("4".matches(regex));//false
        regex = "[abc][0-9]";//有两个字符,第一个字符是abc中的任意一个,第二个字符:是数字字符
        System.out.println("a".matches(regex));//false
        System.out.println("c9".matches(regex));//true
        regex = "[^bcd]";//除了bcd以外的任意一个字符都可以
        System.out.println("c".matches(regex));//false
        regex = "[a-z][A-Z][0-9]";//3个字符
        System.out.println("aa9".matches(regex));//false
        regex = "[a-zA-Z0-9]";//一个字符
        System.out.println("aA9".matches(regex));
        //邮编的正则表达式:6个数字
        regex = "[0-9][0-9][0-9][0-9][0-9][0-9]";
        System.out.println("111111".matches(regex));//true
        System.out.println("01234".matches(regex));//false
        System.out.println("qwi123".matches(regex));//false
2.预定义字符集

. 任意一个字符
\d 数字
\w 单词字符相当于[a-z A-Z 0-9]
\s 空白字符
\D 非
\W 非
\S 非

regex = "\\w";//\w:单词字符,字母,数字,下划线
        System.out.println("1A".matches(regex));//false
        System.out.println("_".matches(regex));//true
        regex = "\\d";//数字字符
        System.out.println("12".matches(regex));//false
        regex = "\\s";
        System.out.println("  ".matches(regex));//false
        regex = "\\W";
        System.out.println("5".matches(regex));//false
        System.out.println("_".matches(regex));//false
        System.out.println("^".matches(regex));//true
        regex = "\\D";
        System.out.println("U".matches(regex));//true
        System.out.println("7".matches(regex));//false
        System.out.println("A".matches(regex));//true
        regex = "\\w\\D\\s";
        System.out.println("11 ".matches(regex));//false
        System.out.println("1U ".matches(regex));//true
        System.out.println("_* ".matches(regex));//true
        regex = ".";
        System.out.println("a".matches(regex));
        System.out.println("(".matches(regex));
        System.out.println("&".matches(regex));
3.数词

x? 0个或一个x
x* 0个或任意多
x+ 大于等于1个
x{n} n个x
x{n,} n到任意多
x{n,m} n-m个

regex = "\\d{5}\\w{2,}";
        System.out.println("12345678".matches(regex));//true
        System.out.println("12345_6".matches(regex));//true
        System.out.println("12345UA_78".matches(regex));//true
        regex = "\\w?\\d+\\s?";
        System.out.println("56 ".matches(regex));//true
        System.out.println("6".matches(regex));//true
        System.out.println("A1234".matches(regex));//true
        System.out.println("A1234 ".matches(regex));//true
        regex = "^1\\d{7,11}@qq.com$";//^:以什么开头 &:以什么结尾
        System.out.println("[email protected]".matches(regex));//true
        System.out.println("[email protected]".matches(regex));//false
        regex = "^a\\w+\\d8$";
        System.out.println("a_A12348".matches(regex));//true
        System.out.println("aQbdf1238".matches(regex));//true
        System.out.println("a_U8".matches(regex));//false

^以什么开头
$以什么结尾
{}表示数量
[]表示字符集

你可能感兴趣的:(java——正则表达式)