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 8549369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:42:01+00:00 2026-06-11T13:42:01+00:00

I am converting a string by replacing all spaces to _ and if there

  • 0

I am converting a string by replacing all spaces to “_” and if there is “_” in actual string , I am converting it to “\_”. If have a string like “this is test _string” result will be “this_is_test_\_string”, now I want to use java regex to get back original string
“this is test _string”. Is it possible to achieve using java regex ?. Please help me out.

  • 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-11T13:42:02+00:00Added an answer on June 11, 2026 at 1:42 pm

    No, it is not possible to get back the original string because you did not escape backslash which makes it ambiguous whether "\\_" came from "_" or "\\ ".

    If you had done

    1. Replace all occurrences of "\\" with "\\\\"
    2. Replace all occurrences of "_" with "\\_"
    3. Replace all occurrences of " " with "_"

    then you can reverse the process by looking for the tokens "\\\\", "\\_", "_" in a single left to right pass.

    In Java, the first transform is

    stringToEncode.replace("\\", "\\\\").replace("_", "\\_").replace(" ", "_")
    

    and the dual is

    String decode(String stringToDecode) {
      int n = stringToDecode.length();
      StringBuilder out = new StringBuilder(n);
      int decoded = 0;
      for (int i = 0; i < n; ++i) {
        switch (stringToDecode.charAt(i)) {
          case '\\':
            out.append(stringToDecode, decoded, i);
            decoded = ++i;
            break;
          case '_':
            out.append(stringToDecode, decoded, i).append(' ');
            decoded = i+1;
            break;
        }
      }
      return decoded != 0
          ? out.append(stringToDecode, decoded, n).toString()
          : stringToDecode;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gzipped base64 JSON string, after converting this string json_decode() can't decode
I need help converting a string to double with 7 decimals. I have a
I am converting a string like 41.00027357629127, and I am using; Convert.ToSingle(41.00027357629127); or float.Parse(41.00027357629127);
I have a problem converting a string binary to a decimal I was using
There are many ways of converting a String to an Integer object. Which is
I have a problem while converting a string whose value is dd.mm.yyyy to DateTime
I have a weird problem in converting a string to double in .NET 3.5.
I have created a StringToMapConverter for converting string in predefined format to a Map
I need help converting this string --> 20090727 10:16:36:643 to --> 07/27/2009 10:16:36 The
I think this will be better explained with an example I have the following

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.