更换支架时例外 [英] Exception when replacing brackets

查看:50
本文介绍了更换支架时例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我想用下划线替换随机字母(仅第一个字母).为此,我使用以下行:

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?

推荐答案

replaceFirst 使用正则表达式作为第一个参数,并使用正则表达式()是特殊的(例如,它们打开和关闭),因此您需要使用例如 \\(或使用表示引用的 \\ Q \\ E 包围它们来对其进行转义.

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.

但是为了避免手动转义,您可以使用 Pattern.quote 方法,例如

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

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

这篇关于更换支架时例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆