android正则并不完全兼容java正则

最近玩android写了个  计算器练手,里面用了一些正则,发现android(2.1)的正则实现与Java的并不完全兼容,看代码: 
Java代码
PrintStream textView = System.out;  
Pattern pattern = null;  
Matcher matcher = null;  
String result = null;  
  
pattern = Pattern.compile("(?<![a-zA-Z])e(?![a-zA-Z])");  
matcher = pattern.matcher("e");  
result = matcher.replaceAll("E");  
textView.append("replace \"(?<![a-zA-Z])e(?![a-zA-Z])\" with \"e\":\t" + result + "\n\n");  
  
try {  
    //测试否定逆序环视中的无限匹配  
    pattern = Pattern.compile("(?<!\\d+)e");  
    matcher = pattern.matcher("e");  
    result = matcher.replaceAll("E");  
    textView.append("replace \"(?<!\\d+)e\" with \"e\":\t" + result + "\n\n");  
} catch (Exception e) {  
    textView.append("replace \"(?<!\\d+)e\" with \"e\" error:\n" + e + "\n\n");  
}  
  
try {  
    //测试肯定逆序环视中的无限匹配  
    pattern = Pattern.compile("(?<=\\d+)e");  
    matcher = pattern.matcher("e");  
    result = matcher.replaceAll("E");  
    textView.append("replace \"(?<=\\d+)e\" with \"e\":\t" + result + "\n\n");  
} catch (Exception e) {  
    textView.append("replace \"(?<=\\d+)e\" with \"e\" error:\n" + e + "\n\n");  
}  
  
  
try {  
    //测试肯定顺序环视中的无限匹配  
    pattern = Pattern.compile("(?=\\d+)e");  
    matcher = pattern.matcher("e");  
    result = matcher.replaceAll("E");  
    textView.append("replace \"(?=\\d+)e\" with \"e\":\t" + result + "\n\n");  
} catch (Exception e) {  
    textView.append("replace \"(?=\\d+)e\" with \"e\" error:\n" + e + "\n\n");  
}  
  
  
try {  
    //测试否定顺序环视中的无限匹配  
    pattern = Pattern.compile("(?!\\d+)e");  
    matcher = pattern.matcher("e");  
    result = matcher.replaceAll("E");  
    textView.append("replace \"(?!\\d+)e\" with \"e\":\t" + result + "\n\n");  
} catch (Exception e) {  
    textView.append("replace \"(?!\\d+)e\" with \"e\" error:\n" + e + "\n\n");  
}  
  
  
textView.flush();  

android代码:

TextView textView = (TextView)findViewById(R.id.display);  
Pattern pattern = null;  
Matcher matcher = null;  
String result = null;  
  
pattern = Pattern.compile("(?<![a-zA-Z])e(?![a-zA-Z])");  
matcher = pattern.matcher("e");  
result = matcher.replaceAll("E");  
textView.append("replace \"(?<![a-zA-Z])e(?![a-zA-Z])\" with \"e\":\t" + result + "\n\n");  
  
try {  
    //测试否定逆序环视中的无限匹配  
    pattern = Pattern.compile("(?<!\\d+)e");  
    matcher = pattern.matcher("e");  
    result = matcher.replaceAll("E");  
    textView.append("replace \"(?<!\\d+)e\" with \"e\":\t" + result + "\n\n");  
} catch (Exception e) {  
    textView.append("replace \"(?<!\\d+)e\" with \"e\" error:\n" + e + "\n\n");  
}  
  
try {  
    //测试肯定逆序环视中的无限匹配  
    pattern = Pattern.compile("(?<=\\d+)e");  
    matcher = pattern.matcher("e");  
    result = matcher.replaceAll("E");  
    textView.append("replace \"(?<=\\d+)e\" with \"e\":\t" + result + "\n\n");  
} catch (Exception e) {  
    textView.append("replace \"(?<=\\d+)e\" with \"e\" error:\n" + e + "\n\n");  
}  
  
  
try {  
    //测试肯定顺序环视中的无限匹配  
    pattern = Pattern.compile("(?=\\d+)e");  
    matcher = pattern.matcher("e");  
    result = matcher.replaceAll("E");  
    textView.append("replace \"(?=\\d+)e\" with \"e\":\t" + result + "\n\n");  
} catch (Exception e) {  
    textView.append("replace \"(?=\\d+)e\" with \"e\" error:\n" + e + "\n\n");  
}  
  
  
try {  
    //测试否定顺序环视中的无限匹配  
    pattern = Pattern.compile("(?!\\d+)e");  
    matcher = pattern.matcher("e");  
    result = matcher.replaceAll("E");  
    textView.append("replace \"(?!\\d+)e\" with \"e\":\t" + result + "\n\n");  
} catch (Exception e) {  
    textView.append("replace \"(?!\\d+)e\" with \"e\" error:\n" + e + "\n\n");  
}  
 
  
  
   
   
   
   
1、关于第一个例子,原因尚不明确,java可以将e替换为E,而android的替换结果为Ee 
2、android的正则实现  不支持在逆序环视中的无限匹配,即使用*或+,可以使用区间量词{n,m},其中m的的最大值不能为Integer.MAX_VALUE,区间的最大值在本例中可为10,11就不行了:(?<!\\d{1,10})e 
错误堆栈: 
Uncaught handler: thread main exiting due to uncaught exception 
java.lang.RuntimeException: Unable to start activity ComponentInfo{freish.activity/fr... 
(?<!\d+)e 
         ^ 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
at android.app.ActivityThread.access$2200(ActivityThread.java:119) at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 
at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:123) at android.os.Looper.loop(Looper.java:123) 
at android.app.ActivityThread.main(ActivityThread.java:4363) at android.app.ActivityThread.main(ActivityThread.java:4363) 
at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:521) at java.lang.reflect.Method.invoke(Method.java:521) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method) at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.util.regex.PatternSyntaxException: Syntax error U_REGEX_LOOK_BEHIND_L... 
(?<!\d+)e 
         ^ 
at  com.ibm.icu4jni.regex.NativeRegEx.open(Native Method) at com.ibm.icu4jni.regex.NativeRegEx.open(Native Method) 
at java.util.regex.Pattern.compileImpl(Pattern.java:264) at java.util.regex.Pattern.compileImpl(Pattern.java:264) 
at java.util.regex.Pattern.<init>(Pattern.java:239) at java.util.regex.Pattern.<init>(Pattern.java:239) 
at java.util.regex.Pattern.compile(Pattern.java:179) at java.util.regex.Pattern.compile(Pattern.java:179) 
at freish.activity.RegexActivity.onCreate(RegexActivity.java:28) at freish.activity.RegexActivity.onCreate(RegexActivity.java:28)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 
... 11 more ... 11 more 
3、无论是java还是android,顺序环视都可以支持无限匹配

你可能感兴趣的:(android正则并不完全兼容java正则)