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; }
} |