3 回答

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊
這個(gè)怎么處理都行的,不論用什么方法底層其實(shí)都是substring,如果你的<table>和</table>是固定,可以這樣StringUitls.substringBefore("yourstr", "<table>")+StringUitls.substringAfter("yourstr","</table>")具體效果,調(diào)試下即可。

TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊
java里不支持遞歸匹配, 難點(diǎn). 不過我們可以用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); }
結(jié)果:
<table>.fdghfhgfdfgdfgdfg$$$$dsfdsfdsfdsf</table>

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊
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));
}
}
}
添加回答
舉報(bào)