假如有一个字符串 如下:<embed src="F:\Fun\756.swf" width="90" height="70" ></embed><embed src="F:\Fun\519.swf" width="90" height="70" ></embed>
我现在想解析,先循环2次  //这个 我已经解决。然后在循环里, 分别对src="...." 替换成 2个值
最终得出的效果为:
<embed src="aa_01" width="90" height="70" ></embed><embed src="aa_02" width="90" height="70" ></embed>01,02 为2次循环 得到的值。 请问 大家有什么好办法!

解决方案 »

  1.   

    使用正则替换如何,如下这样
    String testString = "<embed src=\"F:\\Fun\\756.swf\" width=\"90\" height=\"70\" ></embed><embed src=\"F:\\Fun\\519.swf\" width=\"90\" height=\"70\" ></embed>";Pattern p = Pattern.compile("(?<=src=\")[^\"]*");
    Matcher m = p.matcher(testString);
    int i=1;
    DecimalFormat format = new DecimalFormat("00");while (m.find()) {
    testString = testString.replace(m.group(), "aa_"+format.format(i++));
    }
    System.out.println(testString);