正则--零宽断言



public static void main(String[] args) {
String pattern = "(?<=\">)(.*?)(?=\\</base:Paragraphs)";
Pattern r = Pattern.compile(pattern);
Matcher m =r.matcher("<base:Paragraphs id=\"p0057\" num=\"0057\"><base:TableDetails num=\"0001\"><base:Table></base:Table></base:TableDetails></base:Paragraphs>");
if (m.find()) {
System.out.println("Found value: " + m.group(0) );
}
}

举例来说(?=exp)代表匹配以exp结尾的字符串,但匹配出来的结果并不带exp,?<=exp)匹配以exp开头的字符串,但结果不带exp

中间的表达式".*?"

".*"(任意字符匹配0次或多次)

"?"(前面的内容匹配0次或1次)

 

参考地址:https://blog.csdn.net/lindiwo/article/details/81012819

 

posted @ 2021-11-01 17:01  爱上-睡觉  阅读(36)  评论(0编辑  收藏  举报