Question

I want to validate a "Hangul-Korean" text in java.

I will be having the "Korean" text in the "String" and calling "matches()" i will be validating the string.

The regular expression is to test whether that text is "Korean" and that Korean string characters length is in some range like 10 to 20.

I don't want to use any other third party solutions.

Thanks in advance.

Was it helpful?

Solution

You can reference a Unicode block or a script for that.

Here's an example with a script:

Pattern p = Pattern.compile("\\p{IsHangul}");
Matcher m = p.matcher("김인재");
while (m.find()) {
    System.out.println("Found: " + m.group());
}

Output

Found: 김
Found: 인
Found: 재

Notes

  • For a reference on Unicode blocks, see here.
  • For a reference on Korean machine characters see here.
  • To validate a range of numbers of characters, use: Pattern p = Pattern.compile("\\p{IsHangul}{min,max}"); where min is your minimum size and max is your maximum size of Hangul characters.
  • The code you posted as a comment to your question will not work (although it will compile). Please find a reference on how to use Java Patterns / Matchers here.

Caveat for legacy Android versions (9 or lower)

  • It seems this methodoogy will not work with Android < 10 and may even crash the runtime.
  • Will not test or add an alternative here
  • The specific rationale may be buried somewhere here or in linked pages
  • Credits to NoHarmDan for the heads up
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top