本帖最后由 justmewei 于 2010-04-12 14:32:52 编辑

解决方案 »

  1.   

    简单的方法是,求取“<title>”和“</title>”在字符串中的位置,然后截取这两个位置之间的内容,content同上。
    如果txt中文本符合xml语法的话,也可以DOM方式读取各个节点的值。
      

  2.   


          String s="<score> 7</score> <title> My View on Fake Commodities</title><content>Last month I bought a pair of shoes from a rather famous shoe-shop. But this month it  has occurred several small seams.</content>";
          Matcher m=Pattern.compile("<(\\w+)>(.+?)</\\1>").matcher(s);
          Map<String,Integer> int_map=new HashMap<String, Integer>();
          Map<String,String> str_map=new HashMap<String, String>();
          while(m.find()){
                if(m.group(2).trim().matches("\\d+")){
                 int_map.put(m.group(1), Integer.parseInt(m.group(2).trim()));
                }
                else{
                 str_map.put(m.group(1), m.group(2));
                }
          }
          System.out.println(int_map);
          System.out.println(str_map);
      

  3.   

    多行的话,把上面正则这么写
    "(?s)<(\\w+)>(.+?)</\\1>"