Monday, November 27, 2017

Java String.replaceAll() -- replace only the Matched String not the REGEX

Every Java programmer must have used the method,


String.replaceAll();


Usage Example


    "this is a test and that is another test".replaceAll("test", "rule");


the output of this execution will be:
"this is a rule and that is another rule"

Twist!!!

The usage of the method seems to be straightforward. However its not.

The first parameter to the method is actually a regular expression. You may fall into it, if the first argument has some special character which has different meaning in REGEX world.

What if you still want to match the expression as whole instead of as regex? You will need to convert it into String literal. You can do this by placing your expression in between Regex Quoted Literals. i.e. \Q <param> \E.


Example:


String test = "<span class=\"colorful\">This is a colorful text.</span>";
String replace_start = "<span class=\"colorful\">";
String replace_end = "</span>";

String expectedOutput = "This is a colorful text.";



Without Quoted Literals:


    String result = test.replace(replace_start, "").replace(replace_end, "");
    Assert.assertEquals(expectedOutput, result);

// This will fail because result = "<span class=\"colorful\">This is a colorful text.</span>"

With Quoted Literals


    String result = test.replace("\\Q" + replace_start + "\\E", "").replace("\\Q" + replace_end + "\\E", "");
    Assert.assertEquals(expectedOutput, result);

This will pass.







No comments:

Post a Comment