Java 判断字符串是否包含中文正则表达式
注意:Java的字符串要先对\做转义……
其次是因为matcher.matches()没用对。这个方法等同于自动在pattern的前后加上^和$,显然这个字符串超过了长度1,所以匹配失败了。用matcher.find()或者matcher.lookingAt()都可以。
例
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1501')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1501>public static boolean isContainChinese(String str) {
Pattern p=Pattern.compile("[u4e00-u9fa5]");
Matcher m=p.matcher(str);
if(m.find())
{
return true;
}
return false;
}
例2
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 = "亲亲宝宝-http://www.jiaochengji.com";
System.out.println(isContainsChinese(input));
input = "http://www.jiaochengji.com";
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;
}
}
您可能感兴趣的文章:
正则表达式判断中文字符的简单方法
php正则表达式匹配中文的二个例子
python如何使用正则表达式
js正则表达式特殊字符过滤代码
正则表达式在网络编程中的运用
javascript正则表达式简明教程
正则表达式,讲的非常详细
正则表达式使用详解
学习javascipt的正则表达式
正则表达式使用详解