微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

中文字符和中文标点符号的正则表达式

匹配中文标点符号: String str="[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]" 该表达式可以识别出: 。 ; , : “ ”( ) 、 ? 《 》 这些标点符号。 匹配中文汉字 String str="[\u4e00-\u9fa5]"; 该表达式可以识别出任何汉字。

\w匹配的仅仅是中文,数字,字母,对于国人来讲,仅匹配中文时常会用到,见下 复制代码 代码如下:

匹配中文字符的正则表达式: [\u4e00-\u9fa5]

或许你也需要匹配双字节字符,中文也是双字节的字符 复制代码 代码如下:

匹配双字节字符(包括汉字在内):[^\x00-\xff]

注:可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class Test
{
    
    public static void main(String[] args)
    {
        // String regEx = "[1]?";
        
        String words = "にほんご(かな)ニホンゴ(カナ)1sdfasdfasdf您的说法撒的发生的阿斯顿发斯蒂芬dsdddd#¥%@#%¥@#%¥";
        String result = patternZh(words);
        System.out.println(result);
    }
    
    private static String patternZh(String words)
    {
        String regEx = "[\u4e00-\u9fa5]?"; // 匹配中文字符的正则表达式
        // String regEx = "[^\\x00-\\xff]?"; //匹配双字节字符(包括汉字在内)
        Pattern pattern = Pattern.compile(regEx,Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(words);
        StringBuffer strBuf = new StringBuffer(0);
        while (matcher.find())
        {
            if (StringUtils.isNotBlank(matcher.group()))
            {
                strBuf.append(matcher.group());
            }
        }
        return strBuf.toString();
    }
}

原文地址:https://www.jb51.cc/regex/358532.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐