如该字符串
String sys = "<div style="word-break:break-all;margin-left:8pt;font-size:9pt;color:#000000;font-family:宋体"><div><img hspace=1 vspace=1 src="{$SYS_IMAGE_PATH$}13.gif"><img hspace=1 vspace=1 src="{$SYS_IMAGE_PATH$}30.gif">今天天气好<img hspace=1 vspace=1 src="{$SYS_IMAGE_PATH$}18.gif">出去公园玩<img hspace=1 vspace=1 src="{$SYS_IMAGE_PATH$}6.gif"></div></div>"
替换成 String sys = "[13]今天天气好[18]出去公园玩[6]"
String sys = "<div style="word-break:break-all;margin-left:8pt;font-size:9pt;color:#000000;font-family:宋体"><div><img hspace=1 vspace=1 src="{$SYS_IMAGE_PATH$}13.gif"><img hspace=1 vspace=1 src="{$SYS_IMAGE_PATH$}30.gif">今天天气好<img hspace=1 vspace=1 src="{$SYS_IMAGE_PATH$}18.gif">出去公园玩<img hspace=1 vspace=1 src="{$SYS_IMAGE_PATH$}6.gif"></div></div>"
替换成 String sys = "[13]今天天气好[18]出去公园玩[6]"
java菜鸟学堂QQ群144648357
private static String res = "";
private static void test1(String str) {
if(str.indexOf("$}") >= 0 || str.indexOf(".gif\">") >= 0) {
if(str.indexOf("$}") >= 0 && str.indexOf(".gif\">") >= 0 && str.indexOf("$}") < str.indexOf(".gif\">")) {
res = res + "[" + str.substring(str.indexOf("$}") + 2, str.indexOf(".gif")) + "]";
str = str.substring(str.indexOf(".gif"));
} else {
if(str.indexOf("<img") >= 0) {
res = res + str.substring(str.indexOf(".gif\">") + 6, str.indexOf("<img"));
str = str.substring(str.indexOf("<img") + 4);
} else {
System.out.println(res);
return;
}
}
test1(str);
} else {
System.out.println(res);
}
}
public static void main(String[] args) {
String sys = "<div style=\"word-break:break-all;margin-left:8pt;font-size:9pt;color:#000000;font-family:宋体\"><div><img hspace=1 vspace=1 src=\"{$SYS_IMAGE_PATH$}13.gif\"><img hspace=1 vspace=1 src=\"{$SYS_IMAGE_PATH$}30.gif\">今天天气好<img hspace=1 vspace=1 src=\"{$SYS_IMAGE_PATH$}18.gif\">出去公园玩<img hspace=1 vspace=1 src=\"{$SYS_IMAGE_PATH$}6.gif\"></div></div>";
test1(sys);
}
}
另外用java来处理HTML文本真麻烦,最好是读进来,不然双引号要手动转义.. String sys = "<div style=\"word-break:break-all;margin-left:8pt;font-size:9pt;color:#000000;" +
"font-family:宋体\"><div><img hspace=1 vspace=1 src=\"{$SYS_IMAGE_PATH$}13.gif\"><img " +
"hspace=1 vspace=1 src=\"{$SYS_IMAGE_PATH$}30.gif\">今天天气好<img hspace=1 vspace=1 src" +
"=\"{$SYS_IMAGE_PATH$}18.gif\">出去公园玩<img hspace=1 vspace=1 src=\"{$SYS_IMAGE_PATH$}6.gif\"></div></div>";
Pattern p = Pattern.compile("\\d{1,4}(?=\\.)|(?<=\\s*>)[\u4E00-\u9FA5]+(?=\\s*<)");
Matcher m = p.matcher(sys);
while(m.find()) {
System.out.println(m.group());
}