Using the Captured Text of a Group within a Pattern
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] argv) throws Exception { String patternStr = "<(\\S+?).*?>(.*?)</\\1>"; Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(""); // Set the input matcher.reset("xx <tag a=b> yy </tag> zz"); // Get tagname and contents of tag boolean matchFound = matcher.find(); String tagname = matcher.group(1); String contents = matcher.group(2); matcher.reset("xx <tag> yy </tag0>"); matchFound = matcher.find(); } }