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.PatternSyn taxExcepti on: Unexpected internal error near index 1
\
^
at java.util.regex.Pattern.er ror(Patter n.java:170 0)
at java.util.regex.Pattern.co mpile(Patt ern.java:1 453)
at java.util.regex.Pattern.<i nit>(Patte rn.java:11 30)
at java.util.regex.Pattern.co mpile(Patt ern.java:8 22)
at java.lang.String.replaceAl l(String.j ava:2190)
at Replace.<init>(Replace.jav a: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%7 Cp%7C %7C%2B%7C %7Ck%7Cu%7Cm%7Ca%7Cr%7C%%7 C2%7C4%7C
Thanks.
my code looks like
String str = "sandeep + kumar";
str = str.replaceAll("\\","%5C")
System.out.println(str);
I am getting this error
java.util.regex.PatternSyn
\
^
at java.util.regex.Pattern.er
at java.util.regex.Pattern.co
at java.util.regex.Pattern.<i
at java.util.regex.Pattern.co
at java.lang.String.replaceAl
at Replace.<init>(Replace.jav
at Replace.main(Replace.java:
Process completed.
And If I try with
str = str.replaceAll("\\\\","%5C
Output comes as
%7Cs%7Ca%7Cn%7Cd%7Ce%7Ce%7
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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%7 Cp%7C %7C%2B%7C %7Ck%7Cu%7Cm%7Ca%7Cr%7C%%7 C2%7C4%7C
you sure, double check your input
> str = str.replaceAll("\\\\","%5C
that should be ok
> Output comes as
%7Cs%7Ca%7Cn%7Cd%7Ce%7Ce%7
you sure, double check your input
if I understand correctly, now the problem is with str.replaceAll("\\\\","%5C ")!
ASKER
I tried withstr = str.replaceAll("\\\\","%5C ");
I am getting
%7Cs%7Ca%7Cn%7Cd%7Ce%7Ce%7 Cp%7C %7C%2B%7C %7Ck%7Cu%7Cm%7Ca%7Cr%7C%%7 C2%7C4%7C
I am getting
%7Cs%7Ca%7Cn%7Cd%7Ce%7Ce%7
ASKER
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 ");
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
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(URLEnco der.encode (str, "UTF-8"));
Please don't repeat earlier posts
You'd be better to use
System.out.println(URLEnco
It looks like your source string has a | between every character
>>
my code looks like
String str = "sandeep + kumar";
>>
That can't be your input string, whatever of the given code pieces you're running
my code looks like
String str = "sandeep + kumar";
>>
That can't be your input string, whatever of the given code pieces you're running
ASKER
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 ");
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
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