replacement.java

来自「运用JSP+tomcat及各种数据库编辑的一些较常用的应用程序」· Java 代码 · 共 26 行

JAVA
26
字号
package com.ideas.parser;

import java.util.regex.*;

public class Replacement {
  public static void main(String[] args) throws Exception {
// Create a pattern to match cat
    Pattern p = Pattern.compile("cat");
// Create a matcher with an input string
    Matcher m = p.matcher("one cat," +
                          " two cats in the yard");
    StringBuffer sb = new StringBuffer();
    boolean result = m.find();
// Loop through and create a new String
// with the replacements
    while (result) {
      m.appendReplacement(sb, "dog");
      result = m.find();
    }
// Add the last segment of input to
// the new String
    m.appendTail(sb);
    System.out.println(sb.toString());
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?