正则表达式(十二)——字符串的替换

时间:2022-11-22 18:46:06

1、查找字符串中的字串

 1 package com.wy.regular;
 2 
 3 import java.util.regex.Matcher;
 4 import java.util.regex.Pattern;
 5 
 6 public class RegularTest {
 7 
 8     public static void main(String[] args) {
 9         /**
10          * 字符串的替换 replacement
11          */
12         Pattern pattern = Pattern.compile("java");
13         Matcher matcher = pattern.matcher("java Java JAVa JaVa I love JAVA you hate Java");
14         // 查找所有的Java
15         while (matcher.find()) {
16             pri(matcher.group());// 只有一个java
17         }
18     }
19 
20     public static void pri(Object object){
21         System.out.println(object);
22     }
23 }

2、忽略大小写

 1 package com.wy.regular;
 2 
 3 import java.util.regex.Matcher;
 4 import java.util.regex.Pattern;
 5 
 6 public class RegularTest {
 7 
 8     public static void main(String[] args) {
 9         /**
10          * 字符串的替换 replacement
13          * 使用compile(String regex, int flags)方法
14          * CASE_INSENSITIVE 大小写不敏感,忽略大小写
15          */
16         Pattern pattern = Pattern.compile("java", Pattern.CASE_INSENSITIVE);
17         Matcher matcher = pattern.matcher("java Java JAVa JaVa I love JAVA you hate Java");
18         /**
19          *  查找所有的Java,结果是:
20          *  java
21          *  Java
22          *  JAVa
23          *  JaVa
24          *  JAVA
25          *  Java
26          */
27         while (matcher.find()) {
28             pri(matcher.group());
29         }
30     }
31 
32     public static void pri(Object object){
33         System.out.println(object);
34     }
35 }

3、替换成大写

 1 package com.wy.regular;
 2 
 3 import java.util.regex.Matcher;
 4 import java.util.regex.Pattern;
 5 
 6 public class RegularTest {
 7 
 8     public static void main(String[] args) {
 9         /**
10          * 字符串的替换 replacement
11          * 使用compile(String regex, int flags)方法
12          * CASE_INSENSITIVE 大小写不敏感,忽略大小写
13          */
14         Pattern pattern = Pattern.compile("java", Pattern.CASE_INSENSITIVE);
15         Matcher matcher = pattern.matcher("java Java JAVa JaVa I love JAVA you hate Java");
16         // 替换所有的Java为大写
17         pri(matcher.replaceAll("JAVA"));// 打印结果:JAVA JAVA JAVA JAVA I love JAVA you hate JAVA
18     }
19 
20     public static void pri(Object object){
21         System.out.println(object);
22     }
23 }

4、单数个替换为大写,双数个替换为小写

 1 package com.wy.regular;
 2 
 3 import java.util.regex.Matcher;
 4 import java.util.regex.Pattern;
 5 
 6 public class RegularTest {
 7 
 8     public static void main(String[] args) {
 9         /**
10          * 字符串的替换 replacement
11          * 使用compile(String regex, int flags)方法
12          * CASE_INSENSITIVE 大小写不敏感,忽略大小写
13          */
14         Pattern pattern = Pattern.compile("java", Pattern.CASE_INSENSITIVE);
15         Matcher matcher = pattern.matcher("java Java JAVa JaVa I love JAVA you hate Java tail");
16         // 单数个替换为大写,双数个替换为小写
17         StringBuffer buffer = new StringBuffer();
18         int i = 0;
19         while (matcher.find()) {
20             i++;
21             if (i%2==0) {
22                 matcher.appendReplacement(buffer, "java");
23             } else{
24                 matcher.appendReplacement(buffer, "JAVA");
25             }
26         }
27         pri(buffer);// 结果:JAVA java JAVA java I love JAVA you hate java,没有结尾的tail
28         matcher.appendTail(buffer);// 添加最后的字符串
29         pri(buffer);// 结果:JAVA java JAVA java I love JAVA you hate java tail
30     }
31 
32     public static void pri(Object object){
33         System.out.println(object);
34     }
35 }