教程集 www.jiaochengji.com
教程集 >  脚本编程  >  java  >  正文 Java 判断字符串是否包含中文正则表达式

Java 判断字符串是否包含中文正则表达式

发布时间:2016-11-30   编辑:jiaochengji.com
教程集为您提供Java 判断字符串是否包含中文正则表达式等资源,欢迎您收藏本站,我们将为您提供最新的Java 判断字符串是否包含中文正则表达式资源
在java中如果我们要判断字符或字符串是否是中文或包含中文我们都可以利用\\u4e00-\\u9fa5这样来带正则验证了,下面看两个我收集的实例。

注意: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

<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('copy2404')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2404>

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的正则表达式
正则表达式使用详解

[关闭]
~ ~