提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
在模式中使用组的捕获文本
import java.util.regex.Matcher; import java.util.regex.Pattern;// 来 自 时 代 J a v a 公 众 号 - nowjava.com public class Main { public static void main(String[] args) throws Exception { // Compile regular expression with a back reference to group 1 String patternStr = "<(\\S+?).*?>(.*?)</\\1>"; Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(""); // Set the input matcher.reset("test this is test xx <tag a=b> yy test test</tag> zz"); // Get tagname and contents of tag// from 时 代 Java - nowjava.com boolean matchFound = matcher.find(); System.out.println(matchFound); String tagname = matcher.group(1); System.out.println(tagna