2

Well I want to call a

String newString = oldString.replaceAll("}","");

but I'm getting error with the } I have tried with:

String newString = oldString.replaceAll("\\}\\","");
String newString = oldString.replaceAll("\}\","");
String newString = oldString.replaceAll("//}//","");
String newString = oldString.replaceAll("/}/","");

and none of them works. How could I do that?

Thanks

Here's the error:

10-19 12:17:44.907: W/System.err(7030): java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 1:
10-19 12:17:44.907: W/System.err(7030): }
10-19 12:17:44.907: W/System.err(7030):  ^
10-19 12:17:44.907: W/System.err(7030):     at java.util.regex.Pattern.compileImpl(Native Method)
10-19 12:17:44.907: W/System.err(7030):     at java.util.regex.Pattern.compile(Pattern.java:400)
10-19 12:17:44.907: W/System.err(7030):     at java.util.regex.Pattern.<init>(Pattern.java:383)
10-19 12:17:44.907: W/System.err(7030):     at java.util.regex.Pattern.compile(Pattern.java:374)
10-19 12:17:44.907: W/System.err(7030):     at java.lang.String.replaceAll(String.java:1784)
10-19 12:17:44.907: W/System.err(7030):     at com.rotaryheart.MainActivity$1.onClick(MainActivity.java:70)
10-19 12:17:44.907: W/System.err(7030):     at android.view.View.performClick(View.java:4084)
10-19 12:17:44.907: W/System.err(7030):     at android.view.View$PerformClick.run(View.java:16966)
10-19 12:17:44.907: W/System.err(7030):     at android.os.Handler.handleCallback(Handler.java:615)
10-19 12:17:44.907: W/System.err(7030):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-19 12:17:44.907: W/System.err(7030):     at android.os.Looper.loop(Looper.java:137)
10-19 12:17:44.907: W/System.err(7030):     at android.app.ActivityThread.main(ActivityThread.java:4940)
10-19 12:17:44.907: W/System.err(7030):     at java.lang.reflect.Method.invokeNative(Native Method)
10-19 12:17:44.907: W/System.err(7030):     at java.lang.reflect.Method.invoke(Method.java:511)
10-19 12:17:44.915: W/System.err(7030):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
10-19 12:17:44.915: W/System.err(7030):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
10-19 12:17:44.915: W/System.err(7030):     at dalvik.system.NativeStart.main(Native Method)

and this is the line MainActivity.java 70

String newString = oldString.replaceAll("}","");

Well this is my onClick call

go.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                try {
                  oldString= "} test } for }";

                        Toast.makeText(getApplicationContext(), "Test for }",
                                Toast.LENGTH_SHORT).show();
                        String newString = oldString.replaceAll("}", "");
                        Toast.makeText(getApplicationContext(), ""+newString , Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
3
  • 5
    your first line works like a charm! what's the problem? Commented Oct 19, 2012 at 16:11
  • With all the double backslashing it takes for a java regex I'd suggest using regexplanet.com/advanced/java/index.html You can use normal regex syntax, test it, and the tool will show the java regex string. I will often validate my regex string there before I put it code. Saves me time and confusion. Commented Oct 19, 2012 at 16:15
  • Added my error please review it Commented Oct 19, 2012 at 16:22

5 Answers 5

12

replaceAll expects a regex and { and } have a special meaning in regexes. You can use the replace method instead (which counter-intuitively does replace all occurences, but takes the string to replace as an argument):

String newString = oldString.replace("}","");
Sign up to request clarification or add additional context in comments.

7 Comments

@assylias.. Well, } is not needed to be escaped.. str = str.replaceAll("}", "a"); works fine for me??
@RohitJain Quickly checking the code, } needs no escape if there is no { before, but does if there is...
@RohitJain Yes agreed - his actual code is probably slightly different.
@assylias. Well just found out that, even if we have { before } in regex, we don't need to escape }.
@RohitJain The op seems to be on Android - it could be a different implementation in the android JDK vs. the desktop Java JDK.
|
2

Well your first code should work fine. You don't need to escape your }.

However, you do need to escape opening braces - {, if you are using it.

So the code: -

str = str.replaceAll("}", ""); 

works fine. The problem you are getting might be because of something you are hiding from us.

If you have some other regex than the one shown above, then we can't see exactly what the problem is.

Ok, I tried it with your given string: -

   String str = "} test } for }";
   str = str.replaceAll("}", "");
   System.out.println(str);

OUTPUT: -

 test  for 

As you see I am getting the requried output, but can't understand why this is not working in your code.

But still, you can try using replace() method, and see if it works: -

String newString = oldString.replace("}", "");

5 Comments

Added my onClick, please check it. Thanks
@RotaryHeary. Try using just str.replace("}", ""); and see if it works.
OMG i'm so stupid, I tried like 1 million different things, but I totally forgot about this one... That did the trick :) Thank you so much! You should add it to your answer ;)
@RotaryHeary. That is good. But still can't understand, why your previous code didn't work.. :( Might be some other reason. But its good that you got it worked..
@RotaryHeary. Also for your information, replaceAll() takes the first argument as regex, so you need to escape all special characters. But while using replace() you don't need to do that..
2

Try this:

String newString = oldString.replaceAll("\\}","");

2 Comments

@TedHopp.. This will not make much of a difference.. Just using it without escaping } works fine. Try it out.. However when you use { then you need to escape..
@RohitJain - Yeah, I just discovered that by experimenting. Interesting. I guess it's analogous to how < and > are treated in HTML--the opening character is always special and the closing character is literal unless there's an unmatched opening character for it to pair with.
1

Just do:

.replaceAll("\\{", "");

Comments

1

You need to escape the special character

Try this to escape the brace in the regular expression:

\}

Which would look like this in Java:

String newString = oldString.replace("\\}","");

Hint: replaceAll is the same as replace for most applications

2 Comments

You also need to escape the \` in the source code, and you should either use replaceAll` with the escape or replace without the escape.
I tried to compile it as it is above, but without the escape, and it read the bracket (I intentionally set it up to where it would cause a "reached end of file while parsing" error, and not escaping the brace avoided the error (reading the brace as code)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.