字符串格式<movie  title="电影" src="http://www.qq.com:808/asdsadsd+_#$"/><movie  title="w213213" src="http://www.qq.com/asd/sadsd+_#$"/>,两个movie便签间可以是任意空白字符隔开,截取前先判断输出字符串是否符合格式,如果符合则截取的结果保存到一个HashMap并以title->src这种形式存储。
src要符合url的格式,以http://或https://开头

解决方案 »

  1.   


    public static Map match(String source, String element, String attr,String attr1) {
    Map result = new LinkedHashMap();
    String reg = "<" + element + "[^<>]*?\\s" + attr+ "=['\"]?(.*?)['\"]?\\s.*?"+ attr1+ "=['\"]?(.*?)['\"]?\\s.*?>";
    Matcher m = Pattern.compile(reg).matcher(source);
    while (m.find()) {
    String key = m.group(1);
    String value = m.group(2);
    result.put(key, value);
    }
    return result;
    } public static void main(String[] args) {
     String source =
     "<img  title='w213213' src='http:\\\\www.qq.com/asd/sadsd+_#$' />";
     Map map = match(source, "img", "title","src");
     Set<Map.Entry> set = map.entrySet();
            for (Iterator<Map.Entry> it = set.iterator(); it.hasNext();) {
                Map.Entry entry = (Map.Entry) it.next();
                System.out.println(entry.getKey() + "--->" + entry.getValue());
            }

    }
      

  2.   


    String html=你的字符串;
    Map<String,String> map=new HashMap<String,String>();
    Matcher m=Pattern.compile("(?s)<movie\\s+title=\"(.*?)\"\\s+src=\"((http(s?)://(.*?)))\"(.*?)/>").matcher(html);
    while(m.find()){
       map.put(m.group(1),m.group(2));
       System.out.println(m.group(1)+":"+m.group(2));
    }