提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用\\ 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