Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8218515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:49:14+00:00 2026-06-07T12:49:14+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:49:15+00:00Added an answer on June 7, 2026 at 12:49 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am starting to learn c#, but i don't know when to use new
I am just starting to learn javascript, so I don't have the skills to
I'm starting to learn about async / await in C# 5.0, and I don't
I am just starting to learn Python, but I have already run into some
So I have starting to learn Qt 4.5 and found the Signal/Slot mechanism to
I am just starting to learn a bit about the entity framework and don't
Just starting to learn scala for a new project. Have got to the point
Whilst starting to learn lisp, I've come across the term tail-recursive . What does
I am starting to learn python. I have gone through several tutorials and now
I'm just starting to learn Python and have heard about the GIL and how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.