![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e439. Using the Captured Text of a Group within a PatternIt 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 // 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
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
© 2002 Addison-Wesley. |