黑马程序员------正则表达式
1.1 正则表达式:符合一定规则的表达式。
作用:用于专门操作字符串。
特点:用于一些特定的符号来表示一些代码操作。这样就简化书写。
所以学习正则表达式,就是在学习一些特殊符号的使用。
好处:可以简化对字符串的复杂操作。
弊端:符号定义越多,正则越长,阅读性越差。
1.2 正则表达式常用构造摘要
字符类
[abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围)
预定义字符类
. 任何字符(与行结束符可能匹配也可能不匹配)
\d 数字:[0-9]
\D 非数字: [^0-9]
\s 空白字符:[ \t\n\x0B\f\r]
\S 非空白字符:[^\s]
\w 单词字符:[a-zA-Z_0-9]
\W 非单词字符:[^\w]
Greedy 数量词
X? X,一次或一次也没有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次
X{n,m} X,至少 n 次,但是不超过 m 次
边界匹配器
^ 行的开头
$ 行的结尾
\b 单词边界
\B 非单词边界
\A 输入的开头
\G 上一个匹配的结尾
\Z 输入的结尾,仅用于最后的结束符(如果有的话)
\z 输入的结尾
1.3 具体操作功能:
1,匹配:String matches方法。用规则匹配整个字符串,只要有一处不符合规则,就匹配结束,返回false。
2,切割:String split();
3,替换:String replaceAll(regex,str);如果regex中有定义组,可以在第二参数中通过$符号获取正则表达式中的已有的组
4,获取:将字符串中的符合规则的子串取出。
示例1:
1 class RegexDemo
2 {
3 public static void main(String[] args)
4 {
5 //demo();
6 //System.out.println((char)11);
7 //checkTel();
8
9 //splitDemo("zhangsan.lisi.wangwu","\\.");
10 //splitDemo("c:\\abc\\a.txt","\\\\");
11
12 //splitDemo("erkktyqqquizzzzzo","(.)\\1+");//按照叠词完成切割。为了可以让规则的结果被重用
13 //可以将规则封装成一个组。用()完成。组的出现都有编号。
14 //从1开始。 想要使用已有的组可以通过 \n(n就是组的编号)的形式来获取。
15
16 String str = "wer1389980000ty1234564uiod234345675f";//将字符串中的数组替换成#。
17
18 //replaceAllDemo(str,"\\d{5,}","#");
19
20 String str1 = "erkktyqqquizzzzzo";//将叠词替换成$. //将重叠的字符替换成单个字母。zzzz->z
21 replaceAllDemo(str1,"(.)\\1+","$1");
22
23
24 }
25
26 public static void replaceAllDemo(String str,String reg,String newStr)
27 {
28 str = str.replaceAll(reg,newStr);
29
30 System.out.println(str);
31 }
32
33
34
35 public static void splitDemo(String str,String reg)
36 {
37
38 //String reg = " +";//按照多个空格来进行切割
39 String[] arr = str.split(reg);
40 System.out.println(arr.length);
41 for(String s : arr)
42 {
43 System.out.println(s);
44 }
45 }
46
47
48
49
50 /*
51 匹配
52 手机号段只有 13xxx 15xxx 18xxxx
53
54 */
55 public static void checkTel()
56 {
57 String tel = "16900001111";
58 String telReg = "1[358]\\d{9}";
59 System.out.println(tel.matches(telReg));
60 }
61
62 public static void demo()
63 {
64 String str = "b23a23456789";
65
66 String reg = "[a-zA-Z]\\d*";
67
68 boolean b= str.matches(reg);
69 System.out.println(b);
70 }
71 public static void checkQQ()
72 {
73 String qq = "123a454";
74
75 String regex = "[1-9]\\d{4,14}";
76
77 boolean flag = qq.matches(regex);
78 if(flag)
79 System.out.println(qq+"...is ok");
80 else
81 System.out.println(qq+"... 不合法");
82
83 }
84
85 }
示例2:
获取:将字符串中的符合规则的子串取出。
操作步骤:
1,将正则表达式封装成对象。
2,让正则对象和要操作的字符串相关联。
3,关联后,获取正则匹配引擎。
4,通过引擎对符合规则的子串进行操作,比如取出。
1 import java.util.regex.*;
2
3 class RegexDemo2
4 {
5 public static void main(String[] args)
6 {
7 getDemo();
8 }
9 public static void getDemo()
10 {
11 String str = "ming tian jiu yao fang jia le ,da jia。";
12 System.out.println(str);
13 String reg = "\\b[a-z]{4}\\b";
14
15 //将规则封装成对象。
16 Pattern p = Pattern.compile(reg);
17
18 //让正则对象和要作用的字符串相关联。获取匹配器对象。
19 Matcher m = p.matcher(str);
20
21 //System.out.println(m.matches());//其实String类中的matches方法。用的就是Pattern和Matcher对象来完成的。
22 //只不过被String的方法封装后,用起来较为简单。但是功能却单一。
23
24 //boolean b = m.find();//将规则作用到字符串上,并进行符合规则的子串查找。
25 //System.out.println(b);
26 //System.out.println(m.group());//用于获取匹配后结果。
27
28
29 //System.out.println("matches:"+m.matches());
30 while(m.find())
31 {
32 System.out.println(m.group());
33 System.out.println(m.start()+"...."+m.end());
34 }
35 }
36 }
37
例子3:需求:将下列字符串转成:我要学编程.
到底用四种功能中的哪一个呢?或者哪几个呢?
思路方式:
1,如果只想知道该字符是否对是错,使用匹配。
2,想要将已有的字符串变成另一个字符串,替换。
3,想要按照自定的方式将字符串变成多个字符串。切割。获取规则以外的子串。
4,想要拿到符合需求的字符串子串,获取。获取符合规则的子串。
1 class RegexTest
2 {
3 public static void main(String[] args)
4 {
5 test_1();
6
7 }
8
9 public static void test_1()
10 {
11 String str = "我我...我我...我要..要要...要要...学学学....学学...编编编...编程..程.程程...程...程";
12 /*
13 将已有字符串变成另一个字符串。使用 替换功能。
14 1,可以先将 . 去掉。
15 2,在将多个重复的内容变成单个内容。
16 */
17 str = str.replaceAll("\\.+","");
18 System.out.println(str);
19
20 str = str.replaceAll("(.)\\1+","$1");
21
22 System.out.println(str);
23
24 }
25
26 }
示例4:示例网页爬虫(蜘蛛)
1 import java.io.*;
2 import java.util.regex.*;
3 import java.net.*;
4 import java.util.*;
5 class RegexTest2
6 {
7 public static void main(String[] args) throws Exception
8 {
9 getMails_1();
10 }
11
12
13 public static void getMails_1()throws Exception
14 {
15 URL url = new URL("http://192.168.1.254:8080/myweb/mail.html");
16
17 URLConnection conn = url.openConnection();
18
19 BufferedReader bufIn = new BufferedReader(new InputStreamReader(conn.getInputStream()));
20
21 String line = null;
22
23 String mailreg = "\\w+@\\w+(\\.\\w+)+";
24 Pattern p = Pattern.compile(mailreg);
25
26
27
28 while((line=bufIn.readLine())!=null)
29 {
30 Matcher m = p.matcher(line);
31 while(m.find())
32 {
33 System.out.println(m.group());
34 }
35 }
36 }
37
38 /*
39 获取指定文档中的邮件地址。
40 使用获取功能。Pattern Matcher
41 */
42 public static void getMails()throws Exception
43 {
44 BufferedReader bufr =
45 new BufferedReader(new FileReader("mail.txt"));
46
47 String line = null;
48
49 String mailreg = "\\w+@\\w+(\\.\\w+)+";
50 Pattern p = Pattern.compile(mailreg);
51
52
53
54 while((line=bufr.readLine())!=null)
55 {
56 Matcher m = p.matcher(line);
57 while(m.find())
58 {
59 System.out.println(m.group());
60 }
61 }
62 }
63 }