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.
I am converting a string by replacing all spaces to _ and if there
Share
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
"\\"with"\\\\""_"with"\\_"" "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
and the dual is