Matching a Word Boundary using \\b to get \b - Java Regular Expressions

Java examples for Regular Expressions:Match

Description

Matching a Word Boundary using \\b to get \b

Demo Code

public class Main {
  public static void main(String[] args) {
    // Prepare regular expression. Use \\b to get \b inside the string.
    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.println("Replacement String: " + replacementStr);
    System.out.println("New String: " + newStr);
  }//  www  . j a v  a 2 s  .com
}

Result


Related Tutorials