3 回答

TA贡献1796条经验 获得超4个赞
这个怎么处理都行的,不论用什么方法底层其实都是substring,如果你的<table>和</table>是固定,可以这样StringUitls.substringBefore("yourstr", "<table>")+StringUitls.substringAfter("yourstr","</table>")具体效果,调试下即可。

TA贡献1779条经验 获得超6个赞
java里不支持递归匹配, 难点. 不过我们可以用negative lookahead来做:
@Testpublic void tttttt(){ String str="<table>.fdghfhgfdfgdfgdfg$$<table>dsgfdsf#####dsfkjdshf</table>$$dsfdsfdsfdsf</table>"; str=str.replaceAll("<table>(?:.(?!.*<table>))*?</table>", ""); System.out.println(str); }
结果:
<table>.fdghfhgfdfgdfgdfg$$$$dsfdsfdsfdsf</table>

TA贡献1866条经验 获得超5个赞
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Test {
public static void main(String[] args) {
final String str = "<table>.fdghfhgfdfgdfgdfg$$<table>dsgfdsf#####dsfkjdshf</table>$$dsfdsfdsfdsf</table>";
final Pattern patt = Pattern.compile("<table>[^<]*(<table>[^<]*</table>)[^<]*</table>");
final Matcher matcher = patt.matcher(str);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
}
}
添加回答
举报