How to write regex expression for chinese character

Viewed 3748

I need to know how we compile Chinese characters into the following words 50,001 - 100,000 積分

Eg: English wording:50,001 - 100,000 points. This is written English wordings

Pattern pattern = Pattern.compile("([A-Z]+\\s[A-Z]+)|(\\d+\\s(\\-|or)\\s(\\d|[a-z])+\\s"+Internationalization.getMessage("POINTS_RANGE_FILTER_POINTS")+")"
2 Answers

You may use a \p{L} Unicode property class to match any Unicode letter. Also, your pattern needs a bit of adjustment to match the numbers in their entirety. See a fix below:

\d+(?:,\d{3})*\s*(?:(?:-|or)\s*\d+(?:,\d{3})*\s*)?\p{L}+

See the regex demo

In Java:

String rx = "\\d+(?:,\\d{3})*\\s*(?:(?:-|or)\\s*\\d+(?:,\\d{3})*\\s*)?\\p{L}+";

NOTE: If you do not care much about the regex precision (e.g. when just extracting these substrings), you may shorten the \\d+(?:,\\d{3})* to \d[\d,]*. See another regex demo.

Details

  • \d+ - 1 or more digits
  • (?:,\d{3})* - 0+ consecutive sequences of , and 3 digits
  • \s* - 0+ whitespaces
  • (?:(?:-|or)\s*\d+(?:,\d{3})*\s*)? - 1 or 0 sequences (an optional one) that matches
    • (?:-|or) - a - or or
    • \s* - 0+ whitespaces
    • \d+(?:,\d{3})* - the number pattern as above
    • \s* - 0+ whitespaces
  • \p{L}+ - 1+ Unicode letters.

You can write it as is. Java supports Unicode. Try this line

System.out.println("積分".matches(".分"));

It prints true

Related