【问题标题】:PatternSyntaxException using apache poiPatternSyntaxException 使用 apache poi
【发布时间】:2016-11-17 11:42:57
【问题描述】:

我在两个不同的项目中使用 Apache POI

第一个项目是一个独立的 Java 应用程序。这里一切都很好。

第二个项目是一个 Android 项目。我可以很好地访问 xlsx 的工作簿,但是在评估公式时,它会因异常而崩溃

java.util.regex.PatternSyntaxException: U_ILLEGAL_ARGUMENT_ERROR \P{IsL}
   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 org.apache.poi.ss.formula.functions.TextFunction$5.<init>(TextFunction.java:124)
   at org.apache.poi.ss.formula.functions.TextFunction.<clinit>(TextFunction.java:123)

这是有问题的代码行:

    final Pattern nonAlphabeticPattern = Pattern.compile("\\P{IsL}");

为什么 Android 不接受这个?正如我所说:它在独立的 Java 应用程序上运行良好....

【问题讨论】:

  • 使用final Pattern nonAlphabeticPattern = Pattern.compile("\\P{L}");
  • \\P{IsL} 不是一个完全合法的模式吗?根据甲骨文,它是Categories may be specified with the optional prefix Is: Both \p{L} and \p{IsL} denote the category of Unicode letters. Same as scripts and blocks, categories can also be specified by using the keyword general_category (or its short form gc) as in general_category=Lu or gc=Lu.
  • Android 正在使用 ICU 正则表达式库,Java 有自己的基于 ICU 的正则表达式。参见thisUnicode 脚本、块、类别和二进制属性是用\p\P 结构编写的,就像在Perl 中一样。如果输入具有属性 prop,\p{prop} 匹配,而如果输入具有该属性,\P{prop} 则不匹配。
  • 不要误会我的意思……如果可以的话,我会改变它。它是他们库中 apache poi 代码的一部分
  • 你的意思是你不能改变模式?

标签: java android regex apache-poi


【解决方案1】:

Android 使用的 ICU 正则表达式库与 Java 正则表达式引擎有点不同。

this reference:

Unicode 脚本、块、类别和二进制属性使用 \p\P 结构编写,就像在 Perl 中一样。 \p{prop} 如果输入有属性 prop 则匹配,而\P{prop} 不匹配如果输入有该属性。

因此,模式应该写成

Pattern nonAlphabeticPattern = Pattern.compile("\\P{L}"); 

【讨论】:

  • 谢谢。我设法做到了。我重新编译了 apache poi 源并为我的项目替换了 jar。等等
  • 在 POI 主干中,我们现在重写了 PROPER 函数以完全不使用正则表达式,并且还修复了一些该方法行为不正确的情况,例如关于一些重音字符,请参阅github.com/apache/poi/commit/…
猜你喜欢
  • 2016-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多