java - 使用 apache poi 的 PatternSyntaxException

标签 java android regex apache-poi

我在两个不同的项目中使用 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 应用程序上运行良好....

最佳答案

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

参见 this reference :

Unicode scripts, blocks, categories and binary properties are written with the \p and \P constructs as in Perl. \p{prop} matches if the input has the property prop, while \P{prop} does not match if the input has that property.

因此,模式应该写成

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

关于java - 使用 apache poi 的 PatternSyntaxException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38376190/

相关文章:

java - 如何在 onCreate 中使用 glide 获取位图?

Java 正则表达式通配符不被接受

android - 收到错误 Room 无法验证数据完整性......即使在添加 fallbackToDestructiveMigration() 之后

0=250|18000=300|26000=0.86M 的 C++ 正则表达式

javascript - 你如何用空格和标点符号分割 javascript 字符串?

java - 我的随机数猜谜游戏出现问题

java - 使用嵌入式 Tomcat 的 Web 应用程序只能在 IDE 中运行

java - 表示 xsd 架构中的列表

Java:根据用户输入将变量分配给数组

android - 带有 4 个 TextView 的 ListView