现在碰到一个问题,把Doc文章另存为Html文件后,要把多余的Word标签去掉,只要Html的标签,现在碰到我的问题是正则表达式对汉字不起作用,比如:
<p class=MsoNormal align=center style='mso-margin-top-alt:auto;mso-margin-bottom-alt:
auto;text-align:center;line-height:120%;mso-pagination:widow-orphan'><b><span
style='font-size:12.0pt;line-height:120%;font-family:宋体;mso-bidi-font-family:
宋体;color:black;mso-font-kerning:0pt'>外贸进出口<span lang=EN-US><o:p></o:p></span></span></b></p>现在把str=“<span
style='font-size:12.0pt;line-height:120%;font-family:宋体;mso-bidi-font-family:
宋体;color:black;mso-font-kerning:0pt'>”找到的正则表达式是:"<\\s*span\\s+([^>]*)\\s*>",当str中
如果包含汉字,则不起作用,去掉汉字就可以,这是为什么呀?请各位前辈、兄弟姐妹们帮帮我吧!
<p class=MsoNormal align=center style='mso-margin-top-alt:auto;mso-margin-bottom-alt:
auto;text-align:center;line-height:120%;mso-pagination:widow-orphan'><b><span
style='font-size:12.0pt;line-height:120%;font-family:宋体;mso-bidi-font-family:
宋体;color:black;mso-font-kerning:0pt'>外贸进出口<span lang=EN-US><o:p></o:p></span></span></b></p>现在把str=“<span
style='font-size:12.0pt;line-height:120%;font-family:宋体;mso-bidi-font-family:
宋体;color:black;mso-font-kerning:0pt'>”找到的正则表达式是:"<\\s*span\\s+([^>]*)\\s*>",当str中
如果包含汉字,则不起作用,去掉汉字就可以,这是为什么呀?请各位前辈、兄弟姐妹们帮帮我吧!
String str = "<p class=MsoNormal align=center style='mso-margin-top-alt:auto;" +
"mso-margin-bottom-alt:auto;text-align:center;line-height:120%;" +
"mso-pagination:widow-orphan'> <b> " +
"<span style='font-size:12.0pt;line-height:120%;" +
"font-family:宋体;mso-bidi-font-family:宋体;color:black;mso-font-kerning:0pt'>" +
" 外贸进出口 " +
"<span lang=EN-US> <o:p> </o:p> </span> </span> </b> </p>";
Matcher m = Pattern.compile("<\\s*span\\s+([^>]*)\\s*>").matcher(str);
while(m.find())
System.out.println(m.group(0));
结果:
<span style='font-size:12.0pt;line-height:120%;font-family:宋体;mso-bidi-font-family:宋体;color:black;mso-font-kerning:0pt'>
<span lang=EN-US>
/**
* 测试网友说的不能匹配中文字符
* @throws Exception
*/
public static void regexEncodingTest()throws Exception{
try{
String content = "aa<测试>bb";
String regex = "<(\\w+)>";
PatternCompiler compiler = new Perl5Compiler();
Pattern patter = compiler.compile(regex);
PatternMatcher matcher = new Perl5Matcher();
if(matcher.contains(content, patter)){
MatchResult result = matcher.getMatch();
System.out.println("匹配成功:"+result.group(1));
}
}catch(Exception e){
e.printStackTrace();
}
}
请问您用的JDK是什么版本的呀?我用的是JDK1.4.2,Tomact5.0+Jsp+Oracle
我用了你的代码,在我的环境里测试了一下,只能找到:<span lang=EN-US> 到底是怎么回事呀?
如果这样就可以了:
String str1 = new String(str.getBytes("8859_1"),"utf-8");
Matcher m = Pattern.compile("<\\s*span\\s+([^>]*)\\s*>").matcher(str1);
while(m.find())
SystemLogger.instance().logDebug("output1:" + m.group(0));
2007.12.05 10:13:28 <D> output1:<span style='font-size:12.0pt;line-height:120%;font-family:??;mso-bidi-font-family:??;color:black;mso-font-kerning:0pt'>
2007.12.05 10:13:28 <D> output1:<span lang=EN-US>汉字变成了?号当如果不转换utf-8,则识别不到汉字
String str1 = new String(htmlCode.getBytes("8859_1"),"utf-8");
然后进行正则表达式的替换后,再转换回GB2312后,汉字怎么还是?号呀?
我是这样转换的:
String str2 = new String(str1.getBytes("8859_1"),"GB2312");