Match a Thai Script character in Java
Over last two hours I have a lot of sexy time with Thai Script strings that slipped in my database. They collate mysteriously, mutate when output, do not have natural order and are a disaster.
I want to just ignore any strings with Thai Script characters, but I have no idea how:
Pattern.compile("\\p{Thai}")
fails on init. "[ก-๛]"
- would that ever work? What's the correct wa开发者_如何学Cy?
Thai
is a Unicode block, and Unicode blocks should be specified as \p{In...}
:
Pattern.compile("\\p{InThai}")
You shouldn’t use Unicode blocks when you mean Unicode scripts. For example, a ฿, which is code point U+0E3F THAI CURRENCY SYMBOL BAHT in Unicode, is a \p{Block=Thai}
ᴀᴋᴀ \p{InThai}
character, but it is not a \p{Script=Thai}
ᴀᴋᴀ \p{IsThai}
character. It’s a currency symbol of the \p{Script=Common}
set.
This is especially true for large sets like Greek. There are 18 code points in the Greek block that are not in the Greek script, and there are 250 code points in the Greek script that are not in the Greek block.
Fortunately, you don't have to worry about those here with Thai, since as of Unicode 6.0, only U+0E3F alone is an outlier here. You’re doubly fortunate in this, because standard Java doesn’t support Unicode scripts prior to Java 7; bizarre but true. For Unicode script support in releases earlier than JDK7, you have to use JNI to get at the ICU regex library, just like Google does for Java on Android. There are a lot of benefits to their appoach, though, so even though it’s JNI it may be worth considering.
精彩评论