import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "<ROW[^<]*?>[^<]*<TO>(?=[^<]*412)[^<]*<\\/TO>.*?<\\/ROW>";
final String string = "<ROW num=\"381\">\n"
+ " <TO>8549672167</TO>\n"
+ " <FROM>8936742582</FROM>\n"
+ " <TIME>5/10/2009 19:49:3</TIME><TEXT>Blah Blah Blah</TEXT>\n"
+ "</ROW>\n"
+ "<ROW num=\"382\">\n"
+ " <TO>85496741267</TO>\n"
+ " <FROM>8591903412</FROM>\n"
+ " <TIME>5/10/2009 19:49:37</TIME>\n"
+ " <TEXT>Hme</TEXT>\n"
+ "</ROW>\n"
+ "<ROW num=\"381\">\n"
+ " <TO>8549672167412</TO>\n"
+ " <FROM>8936742582</FROM>\n"
+ " <TIME>5/10/2009 19:49:3</TIME><TEXT>Blah Blah Blah</TEXT>\n"
+ "</ROW>";
final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL | Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html