蔡香满屋
站在牛顿头上吃苹果

学习网址如下:https://docs.oracle.com/javase/8/docs/api/

1.以下这些都是用来描述字符的信息

Character classes
[abc] ab, or c (simple class)
[^abc] Any character except ab, or c (negation)
[a-zA-Z] a through z or A through Z, inclusive (range)
[a-d[m-p]] a through d, or m through p[a-dm-p] (union)
[a-z&&[def]] de, or f (intersection)
[a-z&&[^bc]] a through z, except for b and c[ad-z] (subtraction)
[a-z&&[^m-p]] a through z, and not m through p[a-lq-z](subtraction)

1.其中a[abc]的意思是:第一个字母必须是a,第二个字母是abc其中的一个如:

String str = "ab";

boolean result = str.matches("a[abc]");// 输出为true

而String  str = "abc";

boolean result = str.matches("a[abc]"); // 输出为false,因为第一个字母为a虽然符合,但是后面却有bc两个字母不符合规则,规则是只允许有一个字母是a或者b或者c

---------------------------------------------

2.a[^abc]的意思是:第一个字母必须是a,第二个字母是非abc其中的一个

String str = "aj";

boolean result = str.matches("a[^abc]");// 输出为true

而String str = "aa";

boolean result = str.matches("a[^abc]");// 输出为false,因为第二个字母不能是a,b,c中的一个

---------------------------------------

3.a[a-zA-Z]的意思是:第一个字母是a,第二个任意范围之内的一个字母都可以

String str = "aj";

boolean result = str.matches("a[a-zA-Z]");// 输出为true

而String str = "a;";

boolean result = str.matches("a[a-zA-Z]");// 输出为false,因为第二个字母不能是小写a到小写z或者大写A到大写z中的一个

---------------------------------------------

4.a[a-z&&[^bc]]的意思是:第一个字母是a,第二个是a-z的范围并且不能b,c中的一个

String str = "az"

boolean result = str.matches("a[a-z&&[^bc]]");// 输出为true

而String str = "ab";

boolean result = str.matches("a[a-z&&[^bc]]");// 输出为false,因为第二个字母不能b,c中的一个

---------------------------------

5. .这个点代表任意一个字符,如:

String str = "az";

boolean result = str.matches(".[a-z&&[^bc]]");// 输出为true,.在开头表示第一个字母输入任意一个字符都可以

-------------------------------

6.\d表示 digit数字 [0-9],就是0到9之间的数字

--------------------------------

7.\D表示非数字[^0-9]就是不是0到之间的数字

------------------------------------

8.\w表示一个word单词,如[0-9A-Za-z]即只要是数字或者字母都满足

---------------------------------

9.\W表示非单词[^0-9A-Za-z]即只要不是数字或者字母都满足

===========================================================================

2.如下所有都是用来描述字符出现的次数

1. ?表示出现0-1次如:[0-9]?就是说0到9出现的次数可以是0次或者1次

String str = "0";

boolean result = str.matches("[0-9]?");// 输出为true,0到9只出现1次

String str = "01";

boolean result = str.matches("[0-9]?");// 输出为false,因为0到9出现了两次,不符合规则

--------------------------------------------------------------------

2.*表示0-n次

--------------------------------------------------------------

3.+表示1-n次

-------------------------------------------------------------

4.{n}固定n次,比如说限制只能输入6位邮政编码,规则可以写成[0-9]{6}或者\d{6}

String str = "123456”

boolean result = str.matches("[0-9]{6}");// 输出为true,因为0到9出现了6次

String str = "1234567“

boolean result = str.matches("//d{6}");// 输出为false,因为0到9出现了7次

-----------------------------------

5.{n,}表示至少出现n次

String str = "123456”

boolean result = str.matches("[0-9]{6,}");// 输出为true,因为0到9至少出现了6次

String str = "1234“

boolean result = str.matches("//d{6}");// 输出为false,因为0到9才出现了4次,必须要出现至少6次

--------------------------------------------------------

6.{m,n}表示m到n次之间

=================================================================

3.下面是查找字符的知识

如:在所有的字符串中找寻满足如下规则的的信息,即把邮政编码也就是数字找寻出来

String str = "123456abc332234abc565656abc";

做法如下:

第一步:利用Pattern类创建一个模式,可以理解为是一个正则表达式对象

Pattern pattern = Pattern.compile("\\d{6}"); // 表示0到9之间的数字出现6次,也就是邮政编码

第二步:利用pattern模式对象创建一个匹配器

Matcher matcher = pattern.matcher(str);

第三步:找寻字符串中出现满足上述规格的字符串

while(matcher.find()) {

  System.out.println(matcher.group());// 找到满足字符串格式的那一串文字

}

Character classes
[abc] ab, or c (simple class)
[^abc] Any character except ab, or c (negation)
[a-zA-Z] a through z or A through Z, inclusive (range)
[a-d[m-p]] a through d, or m through p[a-dm-p] (union)
[a-z&&[def]] de, or f (intersection)
[a-z&&[^bc]] a through z, except for b and c[ad-z] (subtraction)
[a-z&&[^m-p]] a through z, and not m through p[a-lq-z](subtraction)
posted on 2019-07-28 11:18  蔡香满屋  阅读(137)  评论(0)    收藏  举报