zoukankan      html  css  js  c++  java
  • 正则表达式(十二)——字符串的替换

    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 }
  • 相关阅读:
    jQuery横向手风琴
    jQuery宽屏游戏焦点图
    手风琴式相册图片展开效果
    鼠标悬停图片分享按钮动画
    jQuery水平滑动菜单
    jQuery图片水平滑动延迟加载动画
    jQuery悬浮焦点图宽屏
    jQuery自定义美化下拉框
    纯CSS3垂直动画菜单
    面向服务与微服务架构
  • 原文地址:https://www.cnblogs.com/stujike/p/9676201.html
Copyright © 2011-2022 走看看