集册 Java实例教程 使用\\ b匹配单词边界以获取\ b

使用\\ b匹配单词边界以获取\ b

欢马劈雪     最近更新时间:2020-01-02 10:19:05

402
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用\\ b匹配单词边界以获取\ b

public class Main {

  public static void main(String[] args) {

    // Prepare regular expression. Use \\b to get \b inside the string.
    /*
    时代Java公众号 - N o w J a  v a . c o m 提 供
    */

    String regex = "\\bapple\\b";

    String replacementStr = "orange";

    String inputStr = "I have an apple and five pineapples";

    String newStr = inputStr.replaceAll(regex, replacementStr);


    System.out.println("Regular Expression: " + regex);

    System.out.println("Input String: " + inputStr);

    System.out.printl
展开阅读全文