为什么Pattern.compile在Android Studio和Java SE中表现不同?

时间:2019-05-18 09:43:05

标签: java android regex

我需要在任意字符串中匹配一个哈希映射。模式为.*("accounts[^}]*?}).*

在AS中编译时会引发错误:

05-18 12:06:46.666 4611-4611/com.mlntdrv.mybudgetpulse E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.mlntdrv.mybudgetpulse, PID: 4611
    java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 19:
    .*("accounts[^}]*?}).*
                       ^
        at java.util.regex.Pattern.compileImpl(Native Method)
        at java.util.regex.Pattern.compile(Pattern.java:411)
        at java.util.regex.Pattern.<init>(Pattern.java:394)
        at java.util.regex.Pattern.compile(Pattern.java:381)
        at com.mlntdrv.mybudgetpulse.LoginActivity.extractAccountsFromLoginResponse(LoginActivity.java:127)
        at com.mlntdrv.mybudgetpulse.LoginActivity.access$100(LoginActivity.java:27)
        at com.mlntdrv.mybudgetpulse.LoginActivity$1$1.onResponse(LoginActivity.java:71)
        at com.mlntdrv.mybudgetpulse.LoginActivity$1$1.onResponse(LoginActivity.java:58)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5292)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
        at dalvik.system.NativeStart.main(Native Method)

Java SE中的同一个正则表达式:

"C:\Program Files\Java\jdk-11.0.1\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3\lib\idea_rt.jar=63488:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\milan\eclipse-workspace\untitled\out\production\untitled com.company.Main
.*("accounts[^}]*?}).* matches -> "accounts":{"1":"main"}

AS代码:

...
    private static final String PATTERN_ACCOUNTS = ".*(\"accounts[^}]*?}).*";
...
        Pattern regex = Pattern.compile(PATTERN_ACCOUNTS);
        Matcher matches = regex.matcher(loginResponse);

        if(matches.find()){
            accounts = g.fromJson(matches.group(1), accounts.getClass());
        }

        return accounts;
...

Java SE代码:

public class Main {
    private static final String PATTERN_ACCOUNTS = ".*(\"accounts[^}]*?}).*";

    public static void main(String[] args) {
    // write your code here
        Pattern regex = Pattern.compile(PATTERN_ACCOUNTS);
        Matcher matches = regex.matcher("blabla, \"accounts\":{\"1\":\"main\"}, blabla");

        if (matches.find()) System.out.println(PATTERN_ACCOUNTS + " matches -> " + matches.group(1));
    }
}

为什么在AS中无法成功编译,但是在Java SE中可以正常编译?我在做错什么吗?

1 个答案:

答案 0 :(得分:1)

如果您避开花括号,它行得通吗?

    private static final String PATTERN_ACCOUNTS = ".*(\"accounts[^}]*?\\}).*";

Android使用与Java SE不同的正则表达式引擎,该引擎需要用括号括起来以进行文字匹配。

更多:Regex pattern error in Android