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));
}
}
}
这个怎么处理都行的,不论用什么方法底层其实都是substring,如果你的$$<table>和</table>$$是固定,可以这样StringUitls.substringBefore("yourstr", "$$<table>") + StringUitls.substringAfter("yourstr", "</table>$$")具体效果,调试下即可。
java里不支持递归匹配, 难点. 不过我们可以用negative lookahead来做:
结果: