Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

While splitting to below code

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

I am getting this error in 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)
like image 549
Gangadhar Nimballi Avatar asked Sep 03 '13 10:09

Gangadhar Nimballi


4 Answers

String#split accepts a regex.

If you don't want to escape the special char +, you can use Pattern#quote:

Returns a literal pattern String for the specified String.


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

+ will be now treated as a regular character and not a metacharacter.

like image 196
Maroun Avatar answered Oct 31 '22 19:10

Maroun


You should escape the + character.

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

Escaping a regex is usually done by \, but in Java \ is represented as \\.

like image 31
Tarsem Singh Avatar answered Oct 31 '22 19:10

Tarsem Singh


+ should be escaped, cause + is used to represent one of the quantifiers

it should be s.split("\\+");

like image 32
sanbhat Avatar answered Oct 31 '22 19:10

sanbhat


Since + is a meta character and have a special meaning in regex You have to escape that meta character

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

oracle docs on the same

like image 31
Suresh Atta Avatar answered Oct 31 '22 19:10

Suresh Atta