Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.regex.PatternSyntaxException on Android

Tags:

regex

android

I am getting a java.util.regex.PatternSyntaxException when trying to call string.split() with the regex (?<=}),(?=\\{) against a string such as "{test},{test}"

This works fine when I run it in a unit test on the JVM (ie. not on Android) but on Android I get the following stack trace:

  java.util.regex.PatternSyntaxException: Syntax error U_REGEX_RULE_SYNTAX near index 5:
        (?<=}),(?=\{)
        ^
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
        at android.app.ActivityThread.access$1500(ActivityThread.java:117)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:130)
        at android.app.ActivityThread.main(ActivityThread.java:3683)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.util.regex.PatternSyntaxException: Syntax error U_REGEX_RULE_SYNTAX near index 5:
        (?<=}),(?=\{)
        ^
        at java.util.regex.Pattern.compileImpl(Native Method)
        at java.util.regex.Pattern.compile(Pattern.java:400)
        at java.util.regex.Pattern.<init>(Pattern.java:383)
        at java.util.regex.Pattern.compile(Pattern.java:374)
        at java.lang.String.split(String.java:2021)
        at java.lang.String.split(String.java:2002)

I'm wondering if the Android runtime doesn't support regex lookaheads, can anyone confirm this or offer any suggestions for solving this?

like image 848
Rupert Bates Avatar asked Jun 16 '11 16:06

Rupert Bates


1 Answers

This android/java regex page indicates that lookbehinds are supported and that you have the correct syntax for it.

I don't see how escaping the { can cause a problem with the regex. Have you tried this form of the expression? string ex = "(?<=\\}),(?=\\{)";

like image 82
agent-j Avatar answered Sep 16 '22 11:09

agent-j