The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.util.regex  [26 examples] > Groups  [5 examples]

e439. Using the Captured Text of a Group within a Pattern

It is possible to use the value of a group within the same pattern. For example, suppose you're trying to extract the text between some XML tags and you don't know what the possible sets of tags are. However, you do know that the tag name appears in both the start and end tags. How do you take the dynamically matched tag name from the start tag and use it in the end tag?

Back references can be used in this scenario. A back reference refers to a capture group (see e436 Capturing Text in a Group in a Regular Expression) within the pattern. It has the form \n where n is a group number starting from 1. The back reference should not be contained in or precede the named group.

    // 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 = matcher.group(1);     // tag
    String contents = matcher.group(2);    //  yy
    
    matcher.reset("xx <tag> yy </tag0>");
    matchFound = matcher.find();           // false

 Related Examples
e436. Capturing Text in a Group in a Regular Expression
e437. Getting the Indices of a Matching Group in a Regular Expression
e438. Using a Non-Capturing Group in a Regular Expression
e440. Using the Captured Text of a Group within a Replacement Pattern

See also: Flags    Lines    Paragraphs    Searching and Replacing    Tokenizing   


© 2002 Addison-Wesley.