我创建了两个模式来编译ls -l
RegexBuddy 中的输出,但在 Android 模式中编译给了我一个错误。在 Java 7 中,它编译得很好。
原始模式是
(^[l,d,-][-,r,w,x]{9})[\t,\s]{1,}([[a-z_][a-z0-9_]{0,30}|[0-9]{1,})[\t,\s]{1,}([[a-z_][a-z0-9_]{0,30}|[0-9]{1,})[\t,\s]{1,}([0-9]{0,})[\t,\s]{1,}([0-9]{4}-[0-9]{1,2}-[0-9]{1,2}\s[0-9]{2}:[0-9]{2})[\t,\s]{1,}(.{1,})
(^[l,d,-][-,r,w,x]{9})[\t,\s]{1,}[0-9]{1,}[\t,\s]{1,}([[a-z_][a-z0-9_]{0,30}|[0-9]{1,})[\t,\s]{1,}([[a-z_][a-z0-9_]{0,30}|[0-9]{1,})[\t,\s]{1,}([0-9]{0,})[\t,\s]{1,}(\w{3}\s[0-9]{1,2}[\t,\s]{1,}([0-9]{1,2}:[0-9]{2}|[0-9]{4}))[\t,\s]{1,}(.{1,})
第一个是为了匹配
-rwxr-xr-x 1 doctor users 399 2011-11-11 13:33 shot.s
或者
-rwxr-xr-x 1 100 100 399 2011-11-11 13:33 file.txt
第二个是为了匹配
用于匹配
-rwxr-xr-x 1 doctor users 399 Nov 22 2011 shot.s
或者
-rwxr-xr-x 1 100 100 399 Nov 22 13:33 shot.s
在代码中:
-
private static final Pattern LS_L = Pattern.compile("(^[l,d,-][-,r,w,x]{9})[\\t,\\s]{1,}([[a-z_][a-z0-9_]{0,30}|[0-9]{1,})[\\t,\\s]{1,}([[a-z_][a-z0-9_]{0,30}|[0-9]{1,})[\\t,\\s]{1,}([0-9]{0,})[\\t,\\s]{1,}([0-9]{4}-[0-9]{1,2}-[0-9]{1,2}\\s[0-9]{2}:[0-9]{2})[\\t,\\s]{1,}(.{1,})");
-
private static final Pattern LS_L_1 = Pattern.compile("(^[l,d,-][-,r,w,x]{9})[\\t,\\s]{1,}[0-9]{1,}[\\t,\\s]{1,}([[a-z_][a-z0-9_]{0,30}|[0-9]{1,})[\\t,\\s]{1,}([[a-z_][a-z0-9_]{0,30}|[0-9]{1,})[\\t,\\s]{1,}([0-9]{0,})[\\t,\\s]{1,}(\\w{3}\\s[0-9]{1,2}[\\t,\\s]{1,}([0-9]{1,2}:[0-9]{2}|[0-9]{4}))[\\t,\\s]{1,}(.{1,})");
第一个扔
02-24 21:14:21.854: E/AndroidRuntime(3072): Caused by: java.util.regex.PatternSyntaxException: Missing closing bracket in character class near index 219:
02-24 21:14:21.854: E/AndroidRuntime(3072): (^[l,d,-][-,r,w,x]{9})[\t,\s]{1,}([[a-z_][a-z0-9_]{0,30}|[0-9]{1,})[\t,\s]{1,}([[a-z_][a-z0-9_]{0,30}|[0-9]{1,})[\t,\s]{1,}([0-9]{0,})[\t,\s]{1,}([0-9]{4}-[0-9]{1,2}-[0-9]{1,2}\s[0-9]{2}:[0-9]{2})[\t,\s]{1,}(.{1,})
02-24 21:14:21.854: E/AndroidRuntime(3072): ^
02-24 21:14:21.854: E/AndroidRuntime(3072): at java.util.regex.Pattern.compileImpl(Native Method)
02-24 21:14:21.854: E/AndroidRuntime(3072): at java.util.regex.Pattern.compile(Pattern.java:400)
02-24 21:14:21.854: E/AndroidRuntime(3072): at java.util.regex.Pattern.<init>(Pattern.java:383)
02-24 21:14:21.854: E/AndroidRuntime(3072): at java.util.regex.Pattern.compile(Pattern.java:374)
第二个给我
02-24 21:00:24.166: E/AndroidRuntime(1366): Caused by: java.util.regex.PatternSyntaxException: Missing closing bracket in character class near index 250:
02-24 21:00:24.166: E/AndroidRuntime(1366): (^[l,d,-][-,r,w,x]{9})[\t,\s]{1,}[0-9]{1,}[\t,\s]{1,}([[a-z_][a-z0-9_]{0,30}|[0-9]{1,})[\t,\s]{1,}([[a-z_][a-z0-9_]{0,30}|[0-9]{1,})[\t,\s]{1,}([0-9]{0,})[\t,\s]{1,}(\w{3}\s[0-9]{1,2}[\t,\s]{1,}([0-9]{1,2}:[0-9]{2}|[0-9]{4}))[\t,\s]{1,}(.{1,})