// 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("xx <tag a=b> yy </tag> zz");

// Get tagname and contents of tag
boolean matchFound = matcher.find();   // true
String tagname =;     // tag
String contents =;    //  yy

matcher.reset("xx <tag> yy </tag0>");
matchFound = matcher.find();           // false

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.