java匹配中文汉字的正则表达式 正则表达式匹配中文先要了解中文的编码
代码如下 复制代码 [u4E00-u9FA5]汉字?[uFE30-uFFA0]全角字符
[u4E00-u9FA5]汉字?[uFE30-uFFA0]全角字符
匹配中文字符的正则表达式: [u4e00-u9fa5] 匹配双字节字符(包括汉字在内):[^x00-xff]
应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)
代码如下 复制代码
String.prototype.len=function(){return this.replace([^x00-xff]/g,"aa").length;}匹配空行的正则表达式:n[s| ]*r
匹配HTML标记的正则表达式:/<(.)>.|<(.*) />/
匹配首尾空格的正则表达式:(^s*)|(s*$)
知道它了我们就好解决了
代码如下 复制代码
public static void regxChinese(){ // 要匹配的字符串 String source = "<span title='5 星级酒店' class='dx dx5'>"; // 将上面要匹配的字符串转换成小写 // source = source.toLowerCase(); // www.111cn.net 匹配的字符串的正则表达式 String reg_charset = "<span[^>]*?title='([0-9]*[\s|\S]*[u4E00-u9FA5]*)'[\s|\S] *class='[a-z]*[\s|\S]*[a-z]*[0-9]*'"; Pattern p = Pattern.compile(reg_charset); Matcher m = p.matcher(source); while (m.find()) { System.out.println(m.group(1)); } } public static void regxChinese(){ // 要匹配的字符串 String source = "<span title='5 星级酒店' class='dx dx5'>"; // 将上面要匹配的字符串转换成小写 // source = source.toLowerCase(); // 匹配的字符串的正则表达式 String reg_charset = "<span[^>]*?title='([0-9]*[\s|\S]*[u4E00-u9FA5]*)'[\s|\S] *class='[a-z]*[\s|\S]*[a-z]*[0-9]*'"; Pattern p = Pattern.compile(reg_charset); Matcher m = p.matcher(source); while (m.find()) { System.out.println(m.group(1)); } }Java的正则表达式是可以匹配中文字符的,同时,用中文字符来写表达式也是可以的
代码如下 复制代码 String reg_charset = “