PatternSyntaxException using apache poi

2019-01-26 20:24发布

问题:

I am using Apache POI in two different projects

The first project is a standalone Java application. Everything is fine here.

The second project is an Android project. I can access the Workbook of an xlsx just fine, but when it comes to evaluating formulas, it crashes with an Exception

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)

This is the code line in question:

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

Why does Android not accept this? As I said: It's working fine on a standalone Java application ....

回答1:

Android is using ICU regex library that is a bit different from Java regex engine.

See 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.

Thus, the pattern should be written as

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