• Matcher中appendReplacement()方法与replaceAll()方法的联系


    https://blog.csdn.net/mjlfto/article/details/53896981

    package com.mjlf.myBatis.accessControl.regex;
    
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    /**
     * Created by a123 on 16/12/27.
     */
    public class Test {
    
        public static void main(String[] args){
            Pattern p = Pattern.compile("java");
            Matcher m = p.matcher("The java book is java program book c");
            StringBuffer sb = new StringBuffer();
    
            if(m.find()){
                m.appendReplacement(sb,"python");
            }
            System.out.println(sb);
    
            if(m.find()){
                m.appendReplacement(sb,"python");
            }
            System.out.println(sb);
    
            if(m.find()){
                m.appendReplacement(sb,"python");
            }
            System.out.println(sb);
    
            System.out.println(m.replaceAll("c++"));
        }
    }
    /**
    The python
    The python book is python
    The python book is python
    The c++ book is c++ program book c
    */
    

    replace all:

    public String replaceAll(String replacement) {
            reset();
            boolean result = find();
            if (result) {
                StringBuffer sb = new StringBuffer();
                do {
                    appendReplacement(sb, replacement);
                    result = find();
                } while (result);
                appendTail(sb);
                return sb.toString();
            }
            return text.toString();
        }
  • 相关阅读:
    python 随机字符串
    Ajax
    Django (Form)
    Django (项目)
    Django (二)
    Django (一)
    Django 学生管理系统
    地理坐标系 与 投影坐标系
    shapefile
    图表绘制工具--Matplotlib 3
  • 原文地址:https://www.cnblogs.com/brxHqs/p/11671217.html
Copyright © 2020-2023  润新知