Java checks whether a string contains a Chinese regular expression.

Source: Internet
Author: User

In java, if we want to determine whether the character or string is Chinese or contains Chinese, we can use \ u4e00-\ u9fa5 for regular expression verification, let's take a look at the two instances I have collected.

Note: Java strings must be escaped first ......

The second reason is that matcher. matches () is useless. This method is equivalent to automatically adding ^ and $ before and after the pattern. Obviously, this string exceeds the length of 1, so the matching fails. You can use matcher. find () or matcher. lookingAt.

Example

The Code is as follows: Copy code

Public static boolean isContainChinese (String str ){
 
Pattern p = Pattern. compile ("[u4e00-u9fa5]");
Matcher m = p. matcher (str );
If (m. find ())
{
Return true;
}
Return false;
}


Example 2

The Code is as follows: Copy code

Package wenhq.com.cn;
Import java. util. regex. Matcher;
Import java. util. regex. Pattern;
Public class test {
Static String regEx = "[u4e00-u9fa5]";
Static Pattern pat = Pattern. compile (regEx );
Public static void main (String [] args ){
String input = "baby-http://www.bKjia. c0m ";
System. out. println (isContainsChinese (input ));
Input = "http://www.bKjia. c0m ";
System. out. println (isContainsChinese (input ));
}
Public static boolean isContainsChinese (String str)
{
Matcher matcher = pat. matcher (str );
Boolean flg = false;
If (matcher. find ()){
Flg = true;
}
Return flg;
}

}

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.