String.replaceAll problem processing "value { { {"

P

Peebster_99

I would like to use String.replaceAll() in order to replace substrings
that equal: "value { { {" with this string: "value { {".

When attempting to invoke replaceAll as follows:
String s = ("value[\\s]{\\s");
output.replaceAll(s ,"value { {");

The following error result below produced at runtime. Using \\s for
whitespace yields similar results. Can someone point out to me what
I'm doing wrong here?
Thanks,

java.util.regex.PatternSyntaxException: Illegal repetition near index
5
value { { {
^
java.util.regex.PatternSyntaxException: Illegal repetition near index
5
value { { {
^
 
T

Tor Iver Wilhelmsen

String s = ("value[\\s]{\\s");

{ is a special character in regular expressions, you need to escape
that too.

(It's used in POSIX character classes and quantifiers, e.g. (foo){1,2}
will match foo and foofoo but not foofoofoo.)

(Removed non-existing-on-proper-newsservers group.)
 
H

hiwa

I would like to use String.replaceAll() in order to replace substrings
that equal: "value { { {" with this string: "value { {".

When attempting to invoke replaceAll as follows:
String s = ("value[\\s]{\\s");
output.replaceAll(s ,"value { {");

The following error result below produced at runtime. Using \\s for
whitespace yields similar results. Can someone point out to me what
I'm doing wrong here?
Thanks,

java.util.regex.PatternSyntaxException: Illegal repetition near index
5
value { { {
^
java.util.regex.PatternSyntaxException: Illegal repetition near index
5
value { { {
^

'{' is a meta character for Java regexp engine. You have to escape
them with two backslashes prepending.
 
M

Mladen Adamovic

Don't forget also that String.replaceAll process its param
String twice, so if you want to to replace "\\" wth something instead of
replace "\\\\" you must write replace "\\\\\\\\\\". Don't ask me why thay
make it that way.
 
H

hiwa

Mladen Adamovic said:
Don't forget also that String.replaceAll process its param
String twice, so if you want to to replace "\\" wth something instead of
replace "\\\\" you must write replace "\\\\\\\\\\". Don't ask me why thay
make it that way.
String.replaceAll process its param String twice
Ugh! That was my heavy headache during a recent development. I'd like
to know where did you get that info? Sun javadoc doesn't have no
mention about it.

//from my working code ... after a muddy struggle:
preBody = preBody.replaceAll("\\\\", "\\\\\\\\");
 
M

Mladen Adamovic

hiwa said:
Ugh! That was my heavy headache during a recent development. I'd like
to know where did you get that info? Sun javadoc doesn't have no
mention about it.

I have same headache and asked three months ago at the c.l.j.p and one man
help me, unfortunatly I don't know his name at the moment, but THANKS TO
HIM!!!

And it was same replaceAll statement as you mention here:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top