本质就是一个字符串,可以指定一些规则,来校验其他字符串。
格式:
String telRegex="编写规则"
“需要做校验的字符串”.matches(telRegex));
字符类:
[abc] 只能是a,b,c
package com.bingyi.regex;
public class RegexDemo {
public static void main(String[] args) {
String regex = "[abc]";
System.out.println("jby".matches(regex));
}
}
[^abc] 除了a,b,c之外的任何字符
package com.bingyi.regex;
public class RegexDemo {
public static void main(String[] args) {
String regex = "[^abc]";
System.out.println("jby".matches(regex));
}
}
[a-zA-Z] a到在 A到Z,包括(范围)
package com.bingyi.regex;
public class RegexDemo {
public static void main(String[] args) {
String regex = "[a-zA-Z]";
System.out.println("jby".matches(regex));
}
}
[a-d[m-p]] a到d,或m通过p:([a-dm-p]联合)
package com.bingyi.regex;
public class RegexDemo {
public static void main(String[] args) {
String regex = "[a-d[m-p]]";
System.out.println("jby".matches(regex));
}
}
[a-z&&[def]] d,e,或f(交集)
package com.bingyi.regex;
public class RegexDemo {
public static void main(String[] args) {
String regex = "[a-z&&[def]]";
System.out.println("jby".matches(regex));
}
}
[a-z&&[^bc]] a到z,除了b和c:([ad-z]减法)
package com.bingyi.regex;
public class RegexDemo {
public static void main(String[] args) {
String regex = "[a-z&&[^bc]]";
System.out.println("jby".matches(regex));
}
}
[a-z&&[^m-p]] a到z,除了m到p:([a-lq-z])
package com.bingyi.regex;
public class RegexDemo {
public static void main(String[] args) {
String regex = "[a-z&&[^m-p]]";
System.out.println("jby".matches(regex));
}
}
预定义字符类
. 任何字符
package com.bingyi.regex;
public class RegexDemo {
public static void main(String[] args) {
String regex = ".";
System.out.println("jby".matches(regex));
}
}
\d 一个数字:[0-9]
package com.bingyi.regex;
public class RegexDemo {
public static void main(String[] args) {
String regex = "\\d";
System.out.println("jby".matches(regex));
}
}
\D 非数字:[^0-9]
package com.bingyi.regex;
public class RegexDemo {
public static void main(String[] args) {
String regex = "\\D";
System.out.println("jby".matches(regex));
}
}
\s 一个空白字符:[ \t\n\x0B\f\r]
package com.bingyi.regex;
public class RegexDemo {
public static void main(String[] args) {
String regex = "\\s ";
System.out.println("jby".matches(regex));
}
}
\S 非空白字符:[^\s]
package com.bingyi.regex;
public class RegexDemo {
public static void main(String[] args) {
String regex = "\\S";
System.out.println("jby".matches(regex));
}
}
\w [a-zA-Z_0-9]:英文、数字、下划线
package com.bingyi.regex;
public class RegexDemo {
public static void main(String[] args) {
String regex = "\\w";
System.out.println("jby".matches(regex));
}
}
\W [^\w]:一个非单词字符
package com.bingyi.regex;
public class RegexDemo {
public static void main(String[] args) {
String regex = "\\W";
System.out.println("jby".matches(regex));
}
}
数量
X? 一次或0次
package com.bingyi.regex;
public class RegexDemo {
public static void main(String[] args) {
String regex = "\\W?";
System.out.println("jby".matches(regex));
}
}
X* 零次或多次
package com.bingyi.regex;
public class RegexDemo {
public static void main(String[] args) {
String regex = "\\W*";
System.out.println("jby".matches(regex));
}
}
X+ 一次或多次
package com.bingyi.regex;
public class RegexDemo {
public static void main(String[] args) {
String regex = "\\W+";
System.out.println("jby".matches(regex));
}
}
X{n} 正好n次
package com.bingyi.regex;
public class RegexDemo {
public static void main(String[] args) {
String regex = "\\W{8}";
System.out.println("jby".matches(regex));
}
}
X{n, } 至少n次
package com.bingyi.regex;
public class RegexDemo {
public static void main(String[] args) {
String regex = "\\W{8,}";
System.out.println("jby".matches(regex));
}
}
X{n,m} 至少n次但不超过m次
package com.bingyi.regex;
public class RegexDemo {
public static void main(String[] args) {
String regex = "\\W{8,11}";
System.out.println("jby".matches(regex));
}
}