Only enter Chinese
/** * 22. Verify Chinese characters* Expression^[/u4e00-/u9fa5]{0,}$ * Describe only Chinese characters* Examples that match Qingqing Yueer*/ @Test public void a1() { Scanner sc = new Scanner(System.in); String input = sc.nextLine(); String regex = "^[//u4e00-//u9fa5]*$"; Matcher m = Pattern.compile(regex).matcher(input); System.out.println(m.find()); sc.close(); }PS: Let’s take a look at the two ways to write regular expressions that want to match Chinese in Java: one is to use unicode Chinese code; the other is to use Chinese characters directly;
example:
(1) String str = "Xing";
String regexStr = "[/u4E00-/u9FA5]";str.regex(regexStr);
(2) String str = "Xing";
String regexStr = "[a-]";str.regex(regexStr);
illustrate:
(1) Most of the people on the Internet who are used to judge Chinese characters now are /u4E00-/u9FA5. This range is just the interval "China, Japan and South Korea unified ideograms", but this is not all. If you want to include them all, you also need their extension sets, radicals, pictograms, inter-note letters, etc.; For details, you can check the simplified Chinese encoding in unicode.
(2) "[I-]"; is the Chinese corresponding to /u4E00-/u9FA5 that was found. Search for specific uniocde2 in Chinese
The above is the example code of Java using regular expressions (regex) to match Chinese. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!