In Java, you infer whether the Chinese string contains English or a number

Source: Internet
Author: User


public static Boolean includingnum (String str) throws exception{

Pattern p = pattern.compile ("[\u4e00-\u9fa5]*[\\d|\\w]+[\u4e00-\u9fa5]*");

or Pattern p = pattern.compile ("[\u4e00-\u9fa5]*[0-9|a-z| A-z]+[\u4e00-\u9fa5]* ");

Matcher M = P.matcher (str);
Boolean f =m.matches ();
return F;

}


Note: in Java Regular:

\\d represents non-numeric
\\d represents a number
\\w denotes non-word
\U4E00-\U9FA5 is the encoding range for Chinese characters
\\w means words
One of the \ is the escape character!!
[\d\d], [\w\w], [\s\s] These three represent no matter what character. public static void Main (string[] args) {String reg = "[\\d\\d]*\" [^\ "]*\" [\\d\\d]* ";String str = "Hello \" five "auxiliary"; Hello, "all five" auxiliaryBoolean f = str.matches (reg);System.out.println (f);str = "\" Five "" ";//" Five "F = str.matches (reg);System.out.println (f);}The results are true;\\s does not represent Chinese characters in Java, remember. Although it is possible to represent Chinese characters on a regular test machine

In Java, you infer whether the Chinese string contains English or a number

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.