3

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 way?

tchrist
  • 78,834
  • 30
  • 123
  • 180
alamar
  • 18,729
  • 4
  • 64
  • 97

2 Answers2

9

Thai is a Unicode block, and Unicode blocks should be specified as \p{In...}:

Pattern.compile("\\p{InThai}") 
axtavt
  • 239,438
  • 41
  • 511
  • 482
  • +1 Just tried a test compile/run to make sure before upvoting and deleting my answer. `Pattern` was happy with the above. – T.J. Crowder Mar 18 '11 at 12:48
  • 1
    @Daniel: What good is a Unicode 3.0 list from a decade ago when Unicode is up to 6.0 as of last year? :( – tchrist Mar 18 '11 at 13:20
5

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.

tchrist
  • 78,834
  • 30
  • 123
  • 180
  • I know the question was about Java, but for Perl, versions later than 5.6 `\p{Script=Thai}` can be written as `\p{Thai}` (`\p{IsThai}` also works for backwards compatibility). – Adam Millerchip Sep 04 '16 at 05:47