代碼如下
[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();
// 匹配的字符串的正則表達式
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的正則表達式是可以匹配中文字符的,同時,用中文字符來寫表達式也是可以的
新聞熱點
疑難解答