6

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?

2
  • 1
    Did you try to escape the '}' character as well? Commented Jun 16, 2011 at 16:49
  • the '}' character doesn't need to be escaped and if you try it it makes the regex illegal Commented Jun 17, 2011 at 10:12

1 Answer 1

1

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 = "(?<=\\}),(?=\\{)";

Sign up to request clarification or add additional context in comments.

1 Comment

Do you know I was SO sure I had tried this, I think it must be my C# background and I was escaping it with a single backslash! Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.