现在碰到一个问题,把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中
如果包含汉字,则不起作用,去掉汉字就可以,这是为什么呀?请各位前辈、兄弟姐妹们帮帮我吧!

解决方案 »

  1.   

    就是str中如果有汉字“宋体”,则正则表达式不起作用!
      

  2.   

    能找到啊,这跟你说的一样.
      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>
      

  3.   

    我用的org.apache.oro.*包,进行匹配,匹配OK!!!Java自带的我觉得也可以匹配,只不过表达式写对了就行
    /**
     * 测试网友说的不能匹配中文字符
     * @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();
    }
    }
      

  4.   

    shan1119,你好:
       请问您用的JDK是什么版本的呀?我用的是JDK1.4.2,Tomact5.0+Jsp+Oracle
      

  5.   

    shan1119,你好:
        我用了你的代码,在我的环境里测试了一下,只能找到:<span       lang=EN-US>     到底是怎么回事呀?
      

  6.   

    编码问题?我也不知道,我的是utf-8,我用的eclipse
      

  7.   

    我也是呀,我用的是eclipse,当我是GB2312的编码,难道和编码有关系吗?
      

  8.   

    但是还没有解决,如果是GB2312的编码,正则表达式如何写呢?
    如果这样就可以了:
    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));
      

  9.   

    上面的解决方案是如果包含汉字,则要转换编码为utf-8,这样非常不方便,是否还有其他的方法,可以直接用正则表达式替换包含汉字的字符串吗?并且编码是GB2312的
      

  10.   

    是的呀!是直接从一个HTML文件中读取出来的!
      

  11.   

    转换过的打印结果:
    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,则识别不到汉字
      

  12.   

    我先转换utf-8编码,
    String str1 = new String(htmlCode.getBytes("8859_1"),"utf-8");
    然后进行正则表达式的替换后,再转换回GB2312后,汉字怎么还是?号呀?
    我是这样转换的:
    String str2 = new String(str1.getBytes("8859_1"),"GB2312");
      

  13.   

    后来发现不转换成utf-8,我转换为8859_1,也可以做替换!