中文字符和中文标点符号的正则表达式
发布时间:2020-12-14 00:41:21 所属栏目:百科 来源:网络整理
导读:匹配中文标点符号: String str="[u3002uff1buff0cuff1au201cu201duff08uff09u3001uff1fu300au300b]" 该表达式可以识别出: 。 ; , : “ ”( ) 、 ? 《 》 这些标点符号。 匹配中文汉字 String str="[u4e00-u9fa5]"; 该表达式可以识别出
匹配中文标点符号: String str="[u3002uff1buff0cuff1au201cu201duff08uff09u3001uff1fu300au300b]" 该表达式可以识别出: 。 ; , : “ ”( ) 、 ? 《 》 这些标点符号。 匹配中文汉字 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(); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |