Link to home
Start Free TrialLog in
Avatar of sangameshrh
sangameshrh

asked on

Problem with ReplaceAll in String

I want to replace all back slashes(\) in a string with %5C.
my code looks like
String str      =      "sandeep + kumar";
str                  =      str.replaceAll("\\","%5C");
System.out.println(str);

I am getting this error
java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
 ^
    at java.util.regex.Pattern.error(Pattern.java:1700)
    at java.util.regex.Pattern.compile(Pattern.java:1453)
    at java.util.regex.Pattern.<init>(Pattern.java:1130)
    at java.util.regex.Pattern.compile(Pattern.java:822)
    at java.lang.String.replaceAll(String.java:2190)
    at Replace.<init>(Replace.java:31)
    at Replace.main(Replace.java:42)

Process completed.


And If I try with
str                  =      str.replaceAll("\\\\","%5C");

Output comes as
%7Cs%7Ca%7Cn%7Cd%7Ce%7Ce%7Cp%7C %7C%2B%7C %7Ck%7Cu%7Cm%7Ca%7Cr%7C%%7C2%7C4%7C

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what is your input string for that output?
The backslash is an escape character in both regex and Java so needs escaping twice
you need to (double) escape the \

> str                  =      str.replaceAll("\\\\","%5C");

that should be ok

> Output comes as
%7Cs%7Ca%7Cn%7Cd%7Ce%7Ce%7Cp%7C %7C%2B%7C %7Ck%7Cu%7Cm%7Ca%7Cr%7C%%7C2%7C4%7C

you sure, double check your input
if I understand correctly, now the problem is with str.replaceAll("\\\\","%5C")!
Avatar of sangameshrh
sangameshrh

ASKER

I tried withstr                  =      str.replaceAll("\\\\","%5C");
I am getting
%7Cs%7Ca%7Cn%7Cd%7Ce%7Ce%7Cp%7C %7C%2B%7C %7Ck%7Cu%7Cm%7Ca%7Cr%7C%%7C2%7C4%7C
I have to these many replaces

str             =      str.replaceAll("%", "%25");
                  str                  =      str.replaceAll("\t","");
                  str             =      str.replaceAll(" ", "%20");
                  str                  =      str.replaceAll("&","%26");
                  str                  =      str.replaceAll("\\#","%23");
                  str                  =      str.replaceAll("\\$","%24");
                  str                  =      str.replaceAll("`","%27");
                  str                  =      str.replaceAll("\\|","%7C");
                  str                  =      str.replaceAll("\\+","%2B");
                  //str                  =      str.replaceAll("\\\\","%5C");
Please post your input...
works fine here, I get

sandeep%20%2B%20kumar



>>you need to (double) escape the \

Please don't repeat earlier posts

You'd be better to use

            System.out.println(URLEncoder.encode(str, "UTF-8"));
It looks like your source string has a | between every character
have a read here about url encoding :)

http://mindprod.com/jgloss/urlencoded.html
>>
my code looks like
String str      =      "sandeep + kumar";
>>

That can't be your input string, whatever of the given code pieces you're running
Thanks for ur inputs
Finally I got it working
It should be
str             =      str.replaceAll("\\%", "%25");
str            =      str.replaceAll("\t","");
str             =      str.replaceAll(" ", "%20");
str            =      str.replaceAll("\\&","%26");
str            =      str.replaceAll("#","%23");
str            =      str.replaceAll("\\$","%24");
str            =      str.replaceAll("`","%27");
str            =      str.replaceAll("\\|","%7C");
str            =      str.replaceAll("\\+","%2B");
str            =      str.replaceAll("\\\\","%5C");
The first one shouldn't need escaping actually
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup Zone:
Accept CEHJ's comment {http:#20154441} as answer.

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

mwvisa1 Experts Exchange Cleanup Volunteer