Java中正则表达式的使用

 1 package com.myregular.test;
 2 
 3 import java.util.Arrays;
 4 import java.util.regex.Matcher;
 5 import java.util.regex.Pattern;
 6 
 7 public class MyRegularTest {
 8 
 9     public static void main(String[] args) {
10         String str1 = "最近来了几个陌生电话,一个是13500000666," + "一个是13611125565," + "一个是15899903312";
11 
12         String str2 = "Java is very easy!";
13 
14         Pattern p1 = Pattern.compile("((13\d)|(15\d))\d{8}");
15         Matcher m = p1.matcher(str1);
16 
17         Pattern p2 = Pattern.compile("\w+");
18         Matcher m2 = p2.matcher(str2);
19 
20         // 目标内容为一个字符串的处理方式--find和group可以找到并输出匹配字符
21         System.out.println("目标内容为一个字符串的处理方式");
22         while (m.find()) {
23             System.out.println(m.group() + "
" + " "
24                     + "字串起始位置: " + m.start() + 
25                     ",其结束位置: " + m.end());
26         }
27 
28         while (m2.find()) {
29             System.out.println(m2.group() + "
" + " "
30                     + "字串起始位置: " + m2.start() + " "
31                             + ",其结束位置: " + m2.end());
32         }
33 
34         System.out.println("---------------------");
35 
36         // 目标内容为字符串数组的处理方式
37         String[] str3 = { "Java has regular expressions in 1.4", 
38                 "regular expressions now expressing in Java",
39                 "Java represses oracular expressions" };
40         Pattern p3 = Pattern.compile("re\w*");
41         Matcher m3 = null;
42         System.out.println("目标内容为字符串数组的处理方式--替换");
43         for (int i = 0; i < str3.length; i++) {
44             if (m3 == null) {
45                 m3 = p3.matcher(str3[i]);
46             } else {
47                 m3.reset(str3[i]);// 重置字符序列
48             }
49             System.out.println(m3.replaceFirst("哈哈:)"));
50             System.out.println(m3.replaceAll("哈哈:)"));
51         }
52         System.out.println("------------------------------------");
53         // 使用String类提供的replaceAll()、replaceFirst()以及split()
54         System.out.println("使用String类提供的replaceAll()、replaceFirst()以及split()");
55 
56         for (String string : str3) {
57             System.out.println(string.replaceFirst("re\w*", "哈哈:)"));
58             System.out.println(string.replaceAll("re\w*", "哈哈:)"));
59             System.out.println(Arrays.toString(string.split(" ")));
60         }
61 
62     }
63 
64 }

执行结果:

 1 目标内容为一个字符串的处理方式
 2 13500000666
 3  字串起始位置: 14,其结束位置: 25
 4 13611125565
 5  字串起始位置: 29,其结束位置: 40
 6 15899903312
 7  字串起始位置: 44,其结束位置: 55
 8 Java
 9  字串起始位置: 0 ,其结束位置: 4
10 is
11  字串起始位置: 5 ,其结束位置: 7
12 very
13  字串起始位置: 8 ,其结束位置: 12
14 easy
15  字串起始位置: 13 ,其结束位置: 17
16 ---------------------
17 目标内容为字符串数组的处理方式--替换
18 Java has 哈哈:) expressions in 1.4
19 Java has 哈哈:) exp哈哈:) in 1.4
20 哈哈:) expressions now expressing in Java
21 哈哈:) exp哈哈:) now exp哈哈:) in Java
22 Java 哈哈:) oracular expressions
23 Java 哈哈:) oracular exp哈哈:)
24 ------------------------------------
25 使用String类提供的replaceAll()、replaceFirst()以及split()
26 Java has 哈哈:) expressions in 1.4
27 Java has 哈哈:) exp哈哈:) in 1.4
28 [Java, has, regular, expressions, in, 1.4]
29 哈哈:) expressions now expressing in Java
30 哈哈:) exp哈哈:) now exp哈哈:) in Java
31 [regular, expressions, now, expressing, in, Java]
32 Java 哈哈:) oracular expressions
33 Java 哈哈:) oracular exp哈哈:)
34 [Java, represses, oracular, expressions]
原文地址:https://www.cnblogs.com/moonpool/p/5878318.html