Java uses regular expressions (regex) to match Chinese instance code _java

Source: Internet
Author: User

can only enter Chinese

/** 
* 22. Verify Chinese character 
* Expression ^[\u4e00-\u9fa5]{0,}$ 
* Describe only Chinese characters 
* Matching example qingqing Moon * * * 
@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: The following Java to match the regular expression of Chinese can be written in two ways: first, the use of Unicode Chinese code, the second is the direct use of Chinese characters;

Cases:

(1) String str = "clear";

String regexstr = "[\u4e00-\u9fa5]";
Str.regex (REGEXSTR);

(2) String str = "clear";

String regexstr = "[A-龥]";
Str.regex (REGEXSTR);

Description

(1) Now most of the online to judge the Chinese character is \u4e00-\u9fa5 this range is just "China, Japan and South Korea unified Ideographic" This interval, but this is not all, if you want to include all, but also their extension set, radicals, pictographic words, note letters and so on; To view the Simplified Chinese encoding in Unicode

(2) "[A-龥]"; is the \u4e00-\u9fa5 corresponding Chinese. Specific Uniocde2 Chinese to query

The above is a small set of Java to introduce the use of regular expressions (regex) to match the Chinese instance code, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.