gpt4 book ai didi

java.util.regex.PatternSyntaxException : Syntax error in regex pattern near index

转载 作者:IT老高 更新时间:2023-10-28 23:27:01 24 4
gpt4 key购买 nike

拆分到以下代码时

String s = "12+12";
String[] sr = s.split("+");

我在 logcat 中收到此错误

09-03 15:57:22.489: E/AndroidRuntime(22029): FATAL EXCEPTION: main
09-03 15:57:22.489: E/AndroidRuntime(22029): java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 1:
09-03 15:57:22.489: E/AndroidRuntime(22029): +
09-03 15:57:22.489: E/AndroidRuntime(22029): ^
09-03 15:57:22.489: E/AndroidRuntime(22029): at java.util.regex.Pattern.compileImpl(Native Method)
09-03 15:57:22.489: E/AndroidRuntime(22029): at java.util.regex.Pattern.compile(Pattern.java:407)
09-03 15:57:22.489: E/AndroidRuntime(22029): at java.util.regex.Pattern.<init>(Pattern.java:390)
09-03 15:57:22.489: E/AndroidRuntime(22029): at java.util.regex.Pattern.compile(Pattern.java:381)
09-03 15:57:22.489: E/AndroidRuntime(22029): at java.lang.String.split(String.java:1832)
09-03 15:57:22.489: E/AndroidRuntime(22029): at java.lang.String.split(String.java:1813)
09-03 15:57:22.489: E/AndroidRuntime(22029): at net.telivo.fiestacancun.ui.calllog.CallLogDetailsFragment$1.onClick(CallLogDetailsFragment.java:171)
09-03 15:57:22.489: E/AndroidRuntime(22029): at android.view.View.performClick(View.java:4240)
09-03 15:57:22.489: E/AndroidRuntime(22029): at android.view.View$PerformClick.run(View.java:17721)
09-03 15:57:22.489: E/AndroidRuntime(22029): at android.os.Handler.handleCallback(Handler.java:730)
09-03 15:57:22.489: E/AndroidRuntime(22029): at android.os.Handler.dispatchMessage(Handler.java:92)
09-03 15:57:22.489: E/AndroidRuntime(22029): at android.os.Looper.loop(Looper.java:137)
09-03 15:57:22.489: E/AndroidRuntime(22029): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-03 15:57:22.489: E/AndroidRuntime(22029): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 15:57:22.489: E/AndroidRuntime(22029): at java.lang.reflect.Method.invoke(Method.java:525)
09-03 15:57:22.489: E/AndroidRuntime(22029): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-03 15:57:22.489: E/AndroidRuntime(22029): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-03 15:57:22.489: E/AndroidRuntime(22029): at dalvik.system.NativeStart.main(Native Method)

最佳答案

String#split接受一个正则表达式

如果您不想逃避 special char +,可以使用Pattern#quote :

Returns a literal pattern String for the specified String.


String sr[] = s.split(Pattern.quote("+"));

+ 现在将被视为常规字符而不是元字符。

关于java.util.regex.PatternSyntaxException : Syntax error in regex pattern near index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18589965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com