user3572325
user3572325

Reputation:

Exception when replacing brackets

Hey I want to replace a random letter (only the first) with an underscore. For that I use following line:

String newSubstring=substring.replaceFirst(randomLetter,"_");

That works fine except when there is a bracket "(" or ")". Then I get the following exception:

06-14 15:29:48.090: E/AndroidRuntime(12466): FATAL EXCEPTION: main
06-14 15:29:48.090: E/AndroidRuntime(12466): Process: com.clozegenerator, PID: 12466
06-14 15:29:48.090: E/AndroidRuntime(12466): java.util.regex.PatternSyntaxException:     Incorrectly nested parentheses in regexp pattern near index 1:
06-14 15:29:48.090: E/AndroidRuntime(12466): )
06-14 15:29:48.090: E/AndroidRuntime(12466):  ^
06-14 15:29:48.090: E/AndroidRuntime(12466):    at java.util.regex.Pattern.compileImpl(Native Method)
06-14 15:29:48.090: E/AndroidRuntime(12466):    at java.util.regex.Pattern.compile(Pattern.java:411)
06-14 15:29:48.090: E/AndroidRuntime(12466):    at java.util.regex.Pattern.<init>(Pattern.java:394)
06-14 15:29:48.090: E/AndroidRuntime(12466):    at java.util.regex.Pattern.compile(Pattern.java:381)
06-14 15:29:48.090: E/AndroidRuntime(12466):    at java.lang.String.replaceFirst(String.java:1804)
06-14 15:29:48.090: E/AndroidRuntime(12466):    at com.clozegenerator.MainActivity.generateCloze(MainActivity.java:138)
06-14 15:29:48.090: E/AndroidRuntime(12466):    at com.clozegenerator.MainActivity.onClick(MainActivity.java:113)
06-14 15:29:48.090: E/AndroidRuntime(12466):    at android.view.View.performClick(View.java:4480)
06-14 15:29:48.090: E/AndroidRuntime(12466):    at android.view.View$PerformClick.run(View.java:18673)
06-14 15:29:48.090: E/AndroidRuntime(12466):    at android.os.Handler.handleCallback(Handler.java:733)
06-14 15:29:48.090: E/AndroidRuntime(12466):    at android.os.Handler.dispatchMessage(Handler.java:95)
06-14 15:29:48.090: E/AndroidRuntime(12466):    at android.os.Looper.loop(Looper.java:157)
06-14 15:29:48.090: E/AndroidRuntime(12466):    at android.app.ActivityThread.main(ActivityThread.java:5872)
06-14 15:29:48.090: E/AndroidRuntime(12466):    at java.lang.reflect.Method.invokeNative(Native Method)
06-14 15:29:48.090: E/AndroidRuntime(12466):    at java.lang.reflect.Method.invoke(Method.java:515)
06-14 15:29:48.090: E/AndroidRuntime(12466):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
06-14 15:29:48.090: E/AndroidRuntime(12466):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
06-14 15:29:48.090: E/AndroidRuntime(12466):    at dalvik.system.NativeStart.main(Native Method)

Any ideas how I can solve that?

Upvotes: 1

Views: 2860

Answers (1)

Pshemo
Pshemo

Reputation: 124265

replaceFirst uses regex as first parameter and in regex ( and ) are special (they open and close groups for instance) so you need to escape them using for instance \\( or surrounding them with \\Q \\E which represents quotation.

But to avoid escaping them manually you can use Pattern.quote method like

String newSubstring=substring.replaceFirst(Pattern.quote(randomLetter),"_");

Upvotes: 7

Related Questions