8

Im starting to learn regex and I don't know if I understand it correctly.

I have a problem with function replaceAll because it does not replace the character in a string that I want to replace.

Here is my code:

public class TestingRegex {
   public static void main (String args[]) {
      String string = "Hel%l&+++o_Wor_++l%d&#";

      char specialCharacters[] = {'%', '%', '&', '_'};

      for (char sc : specialCharacters) {
          if (string.contains(sc + ""))
              string = string.replaceAll(sc + "", "\\" + sc);
      }

      System.out.println("New String: " + string);
   }
}

The output is the same as the original. Nothing changed.

I want the output to be : Hel\%l\&+++o\_Wor\_++l\%d\&\#.

Please help. Thanks in advance.

NinjaBoy
  • 3,715
  • 18
  • 54
  • 69

1 Answers1

11

The reason why it's not working: You need four backslashes in a Java string to create a single "real" backslash.

string = string.replaceAll(sc, "\\\\" + sc);

should work. But this is not the right way to do it. You don't need a for loop at all:

String string = "Hel%l&+++o_Wor_++l%d&#";
string = string.replaceAll("[%&_]", "\\\\$0");

and you're done.

Explanation:

  • [%&_] matches any of the three characters you want to replace
  • $0 is the result of the match, so
  • "\\\\$0" means "a backslash plus whatever was matched by the regex".

Caveat: This solution is obviously not checking whether any of those characters had already been escaped previously. So

Hello\%

would become

Hello\\%

which you would not want to happen. Could this be a problem?

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • 2
    Thanks so much for taking time for answering and explaining your answer. I am now starting to understand regex. – NinjaBoy Jul 12 '12 at 07:41
  • 2
    @NinjaBoy: You're welcome: Check out http://www.regular-expressions.info - a great regex tutorial. – Tim Pietzcker Jul 12 '12 at 07:42
  • The reason I used loop because I assume that the special characters are from an enum field. – NinjaBoy Jul 12 '12 at 07:45
  • @NinjaBoy: You'll need to be careful then when constructing your character class. Some characters (especially `^`, `]` and `-`) have special meaning inside character classes and need to be escaped or placed into certain special positions. Check out the section on character classes under the link I posted above for more info. – Tim Pietzcker Jul 12 '12 at 07:49