正则表达式 | 说明 |
[abc] | a、b、c中任意一个字符 |
[^abc] | 除了abc的任意一个字符 |
[a-z] | a-z中任意一个字符 |
[a-zA-Z0-9] | a-zA-Z0-9中任意一个字符 |
[a-z&&[^bc]] | a-z中除了bc的任意一个字符 |
知识点案例:
String regex1="[a-z]";
String regex2="[^a-z]";
String regex3="[a-z&&[^bc]]";
System.out.println("a".matches(regex1)); //true
System.out.println("0".matches(regex1)); //false
System.out.println("a".matches(regex2)); //false
System.out.println("0".matches(regex2)); //true
System.out.println("a".matches(regex3)); //true
System.out.println("b".matches(regex3)); //false
正则表达式 | 说明 |
. | 任意一个字符 |
\d | 任意一个数字字符,相当于[0-9] |
\w | 单词字符,相当于[a-zA-Z0-9_] |
\s | 空白字符,相当于[\t\n\f\r\x0B](x0B:匹配值为十六进制的ASCII字符;\r:回车;\f:换页;\n:换行;\t:制表) |
\D | 非数字字符 |
\W | 非单词字符 |
\S | 非空白字符 |
知识点案例:
String regex1="\\d";
String regex2="\\D";
String regex3="\\s";
String regex4="\\S";
String regex5="\\w";
String regex6="\\d";
System.out.println("1".matches(regex1)); //true
System.out.println("1".matches(regex2)); //false
System.out.println(" ".matches(regex3)); //true
System.out.println(" ".matches(regex4)); //false
System.out.println("_".matches(regex5)); //true
System.out.println("_".matches(regex6)); //false
正则表达式 | 说明 |
x? | 0个或1个x |
x* | 0个或任意多个x |
x+ | 1个或任意多个x |
x{n} | n个x |
x{n,} | n个到任意多个x |
x{m,n} | m个到n个x |
知识点案例:
-6位数字
-第一种匹配 [0-9] [0-9] [0-9] [0-9] [0-9] [0-9]
-简化1 \d\d\d\d\d\d
-简化2 \d{6}
知识点:分组“()”:
圆括号表示分组,圆括号内的正则表达式为一个整体,分组可以用“|”表示“或”
案例:
匹配手机号前面的区号:
(\+86|0026)?\s?\d{11}
表示+86或者0086出现其中一个后接1个或0个空白字符后接11位任意数字
知识点:“^”和“$”
边界匹配
-^代表字符串开始
-$代表字符串结束
案例:
匹配用户名规则:从头到尾连续8~10个单词字符
方法一:\w{8,10}
方法二:^\w{8,10}$
对于字符串“abcd1234_abcd”,方法一通过,方法二是不通过的,因为方法一只看到前8到前10个字符,对于后面的字符不予匹配,所以只要前8到前10个字符满足则通过验证,而方法二则是匹配8-10个字符,从前8到前10,是有边界结束的,因为并不是正好有8-10个字符,所以匹配失败,验证不通过。但是在我们用eclipse验证时,发现方法一在验证时也是不予通过的,原因会在接下来进行说明
String包含的正则方法
1.知识点:matches方法
作用:正则表达式与字符串进行匹配,成功返回true
boolean matches(String regex)
案例
String regex="[\\w.]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z0-9]{2,4}";
String email="wangcongming@oraclewdp-china.com";
System.out.println(email.matches(regex)); //true
注意:matches()的参数底层实际上是^regex$
2.知识点:split方法
作用:将字符串按照特定的分隔符拆分成字符串数组,即满足正则表达式的字符串作为分隔符,前后分割成元素,成为字符串数组
String[] split(String regex)
案例:
情况一:
String str="m-a-h-u-a-t-e-n-g";
String[] arr=str.split("-");
System.out.println(Arrays.toString(arr));
结果:[m, a, h, u, a, t, e, n, g]
情况二:
String str="-m-a-h-u-a-t-e-n-g";
String[] arr=str.split("-");
System.out.println(Arrays.toString(arr));
结果:[, m, a, h, u, a, t, e, n, g]
情况三:
String str="m-a-h-u-a-t-e-n-g-";
String[] arr=str.split("-");
System.out.println(Arrays.toString(arr));
结果:[m, a, h, u, a, t, e, n, g]
总结:null也可以作为一个数组元素,但是对于字符串分割来说包前不包后,前面的null包括,后面的null舍去
3.知识点:replaceAll方法
作用:将符合正则表达式的字符串替换为其他字符
String replaceAll(String regex,String replacement)
练习:1.用户名注册:
用户名要求:8~12位字符,不能含有空格,进行匹配提示;
匹配成功,提示此用户名可用,匹配失败,提示此用户名含有非法字符
2.邮箱注册:
邮箱名6~12位的单词字符,后缀的名称2~10位的单词字符,最后接域名
例:javapractice@123.com
代码如下:
public class TextRegex {
public static void main(String[] args) {
String regex1="[\\w&&[^_]]{8,12}";
String regex2="[\\w]{6,12}@[\\w]{2,10}(.com|.cn|.com.cn)";
Scanner sc=new Scanner(System.in);
while(true) {
System.out.println("请输入用户名:");
String username=sc.next();
if(username.matches(regex1)) {
System.out.println("用户名可用!");
break;
}else {
System.out.println("用户名含有非法字符!");
}
}
while(true) {
System.out.println("请输入邮箱");
String mailname=sc.next();
if(mailname.matches(regex2)) {
System.out.println("邮箱可用!");
break;
}else {
System.out.println("邮箱名含有非法字符!");
}
}
}
}