字符类
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28/* 正则表达式:字符类 1.[abc]:代表a或者b,或者c字符中的一个. 2.[^abc]:代表a,b,c以外的任何字符. 3.[a-z]:代表a-z的所有小写字符中的一个. 4.[A-Z]:代表A-Z的所有大写字符中的一个. 5.[0-9]:代表0-9之间的某个数字字符. 6.[a-zA-Z0-9]:代表a-z或者A-Z或者0-9之间的任意一个字符. 7.[a-dm-p]:a到d或者m到p之间的任意一个字符. */ public class Test01 { public static void main(String[] args) { String string = "hAd"; //1.验证字符是否以h开头,以d结尾,中间是a,e,i,o,u中的一个字符 String regex ="h[aeiou]d"; System.out.println("1."+ string.matches(regex)); //2.验证字符是否:以h开头,以d结尾,中间不是:a,e,i,o,u的一个字符. regex = "h[^aeiou]d"; System.out.println("2." + string.matches(regex)); //3.验证字符是否:以h开头,以d结尾,中间是任意的一位小写字母 regex = "h[a-z]d"; System.out.println("3." + string.matches(regex)); //4.验证字符串是否:以h开头,以d结尾,中间是:a-z或者A-Z或者0-9的任意一个字符 regex = "h[a-zA-Z0-9]d"; System.out.println("4." + string.matches(regex)); } }
逻辑运算符
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17/* 正则表达式:逻辑运算符: 1.&&:并且 2.| :或者 */ public class Test01 { public static void main(String[] args) { String str = "hdd"; //1.验证字符串是否:以h开头,以d结尾,中间是a,e,i,o,u的一个字符 String regex = "h[a|e|i|o|u]d"; System.out.println("1." + str.matches(regex)); //2.验证字符串是否:以h开头,以d结尾,中间是除:aeiou外的其它一位小写字符 regex = "h[a-z&&[^aeiou]]d"; System.out.println("2." + str.matches(regex)); } }
预定义字符
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27/* 正则表达式-预定义字符 1.".":匹配任何字符. 2."d":任何数字[0-9]的简写 3."D":任何非数字[^0-9]的简写: 4."s":空白字符:[tnx0Bfr]的简写 5."S":非空字符:[^s]的简写 6."w":单词字符:[a-zA-Z0-9]的简写 7."W":非单词字符:[^w] */ public class Test01 { public static void main(String[] args) { String str = "had"; //1.验证字符串是否:以h开头,以d结尾,中间是任意一个字符 String regex = "h.d"; System.out.println("1." + str.matches(regex)); //2.验证字符串是否是:"had." str = "had."; regex = "had\."; System.out.println("2." + str.matches(regex)); //3.验证一个手机号:以1开头,第二位是:3,5,7,8的某一位.后面是:10位0-9的数字 str = "13811383838"; regex = "1[3578]\d\d\d\d\d\d\d\d\d"; System.out.println("3." + str.matches(regex)); } }
数量词
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22/* 正则表达式-数量词 1.?:0次或1次 2.*:0次到多次 3.+:1次或多次 4.{n}:恰好n次 5.{n,}:至少n次 6.{n,m}:n到m次(n和m都是包含的) */ public class Test01 { public static void main(String[] args) { String str = "13811383838"; //1.验证手机号: String regex = "1[35789]\d{9}"; System.out.println("1." + str.matches(regex)); //2.验证qq号码:1).5-15位; 2).全部是数字; 3).首位不能为0 str = "4324323"; regex = "[1-9]\d{4,14}"; System.out.println(str.matches(regex)); } }
分组括号
用()进行分组
复制代码
1
2
3
4
5
6
7
8
9public class Test01 { public static void main(String[] args) { String str = "FEW27-25D3D-8DFRW-EAFF9-102AB"; //序列号的规则:五段的字符组成,每段:5个:大写字母 + 数字组成 String regex = "([A-Z0-9]{5}-){4}[A-Z0-9]{5}"; System.out.println(str.matches(regex)); } }
String的split方法中使用正则表达式
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13public class Test01 { public static void main(String[] args) { //1.之前的使用方式 String str = "1,张三,男,20"; String[] split = str.split(","); System.out.println(Arrays.toString(split)); //2.split(String 正则表达式)方法的形参是一个:正则表达式 str = "1,,,,张三,,,,男,,,,10"; String[] split1 = str.split(",+"); System.out.println(Arrays.toString(split1)); } }
String中replaceAll方法使用正则表达式
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14public class Test01 { public static void main(String[] args) { //1.之前我们使用的方式 String str = "放的看法和特朗普的金风科技 空间特朗普"; //将字符串中的"特朗普"替换为"*"符号 String newStr = str.replaceAll("特朗普", "*"); System.out.println(newStr); //将字符串中的所有的数字,替换为"*" str = "的恢940复999健康d9jj9更好hfkg8hk65jdf3hg 风格好2看"; newStr = str.replaceAll("\d+", "*"); System.out.println(newStr); } }
最后
以上就是单身康乃馨最近收集整理的关于javase 正则表达式 (小白必看)的全部内容,更多相关javase内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复