不同的正则EX pression结果在Java SE和Android平台 [英] Different regular expression result in Java SE and Android platform

查看:121
本文介绍了不同的正则EX pression结果在Java SE和Android平台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的的Java SE code,运行于PC

 公共静态无效的主要(字串[] args){
    // stringCommaPattern将改变
    //,ABC,DEF,
    // 至
    //,ABCDEF,
    图案stringCommaPattern = Pattern.compile((\\)|,(= [^ \?[,]] * \\));
    字符串数据=\SAN \,\桑坦德银行,\,\NYSE \;
    的System.out.println(数据);
    最后的字符串结果= stringCommaPattern.matcher(数据).replaceAll($ 1);
    的System.out.println(结果);
}
 

我得到预期的结果。

 SAN,桑坦德银行,纽约证券交易所
散,桑坦德银行,纽约证券交易所
 

然而,当涉及到机器人

 模式stringCommaPattern = Pattern.compile((\,\)|(= [^ \?[,] * \,\));
字符串数据=\SAN \,\桑坦德银行,\,\NYSE \;
Log.i(卓,数据);
最后的字符串结果= stringCommaPattern.matcher(数据).replaceAll($ 1);
Log.i(卓,结果);
 

我收到

 SAN,桑坦德银行,纽约证券交易所
散,银行Santandernull,纽约证券交易所
 

任何建议和解决办法,我怎么可以让这个code的行为一样,因为它是在Java SE的?


附加说明:

其它图案产生相同的结果为好。看来, Android正在使用空字符串无与伦比的组,和Java SE采用了无与伦比的组空字符串。

看看下面code。

 公共静态无效的主要(字串[] args){
    //用于在整数位删除逗号。该数字必须设
    //两个字符串之间。替换为$ 1。
    //
    // digitPattern将改变
    //,10
    // 至
    //100000
    最终的模式digitPattern = Pattern.compile((\)|(= [\\研发,] + \));
    字符串数据=\,亿,\;
    的System.out.println(数据);
    最后的字符串结果= digitPattern.matcher(数据).replaceAll($ 1);
    的System.out.println(结果);
}
 

的Java SE

 ,亿
亿
 

的Andr​​oid

 ,亿
100null000null000
 

解决方案

不是一个原因,但作为一种解决方法,你可以用做 appendReplacement 循环自己,而不是的replaceAll

  StringBuffer的结果=新的StringBuffer();
匹配器米= digitPattern.matcher(数据);
而(m.find()){
  m.appendReplacement(结果,(m.group(1)== NULL:$ 1));
}
m.appendTail(结果);
 

这应该双管齐下的JavaSE和Android。

或全部通过改变正则表达式回避的问题

 模式commaNotBetweenQuotes = Pattern.compile((小于?!\),(\)?!);
字符串结果= commaNotBetweenQuotes.matcher(数据).replaceAll();
 

下面的正则表达式匹配只是你要更改的逗号,而不是你要离开完好无损的,所以你可以不带全部更换为需要捕获组。

I have the following Java SE code, which runs on PC

public static void main(String[] args) {
    // stringCommaPattern will change
    // ","abc,def","
    // to
    // ","abcdef","        
    Pattern stringCommaPattern = Pattern.compile("(\",\")|,(?=[^\"[,]]*\",\")");
    String data = "\"SAN\",\"Banco Santander, \",\"NYSE\"";
    System.out.println(data);
    final String result = stringCommaPattern.matcher(data).replaceAll("$1");
    System.out.println(result);
}

I'm getting expected result

"SAN","Banco Santander, ","NYSE"
"SAN","Banco Santander ","NYSE"

However, when comes to Android.

Pattern stringCommaPattern = Pattern.compile("(\",\")|,(?=[^\"[,]]*\",\")");
String data = "\"SAN\",\"Banco Santander, \",\"NYSE\"";
Log.i("CHEOK", data);
final String result = stringCommaPattern.matcher(data).replaceAll("$1");
Log.i("CHEOK", result);

I'm getting

"SAN","Banco Santander, ","NYSE"
"SAN","Banco Santandernull ","NYSE"

Any suggestion and workaround, how I can make this code behaves same as it is at Java SE?


Additional Note :

Other patterns yield the same result as well. It seems that, Android is using null string for unmatched group, and Java SE is using empty string for unmatched group.

Take the following code.

public static void main(String[] args) {
    // Used to remove the comma within an integer digit. The digit must be located
    // in between two string. Replaced with $1.
    //
    // digitPattern will change
    // ",100,000,"
    // to
    // ",100000,"        
    final Pattern digitPattern = Pattern.compile("(\",)|,(?=[\\d,]+,\")");
    String data = "\",100,000,000,\"";
    System.out.println(data);
    final String result = digitPattern.matcher(data).replaceAll("$1");
    System.out.println(result);
}

Java SE

",100,000,000,"
",100000000,"

Android

",100,000,000,"
",100null000null000,"

解决方案

Not a reason why, but as a workaround you could do the appendReplacement loop yourself rather than using replaceAll

StringBuffer result = new StringBuffer();
Matcher m = digitPattern.matcher(data);
while(m.find()) {
  m.appendReplacement(result, (m.group(1) == null ? "" : "$1"));
}
m.appendTail(result);

This should work on both JavaSE and Android.

Or sidestep the problem entirely by changing the regex

Pattern commaNotBetweenQuotes = Pattern.compile("(?<!\"),(?!\")");
String result = commaNotBetweenQuotes.matcher(data).replaceAll("");

Here the regex matches just the commas you want to change, and not the ones you want to leave intact, so you can just replace them all with "" with no need for capturing groups.

这篇关于不同的正则EX pression结果在Java SE和Android平台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆