Задача должна решаться коротким и красивым способом, близким к такому:
Pattern pat = Pattern.compile("<(" + args[0] + ")[^>]*?>.*?(<\\1[^>]*?>.*?</\\1>).*?</\\1>");
Matcher mat = pat.matcher(txt); //txt - String с результатами чтения файла
while (mat.find()) {
System.out.println(mat.group(0));
String t = mat.group(2);
if (t != null) System.out.println(t);
}
Как поставить правильное условие проверки второй группы из приведенного выражения?
максимально близкий вариант получившийся у меня Matcher mat = Pattern.compile("<("+args[0]+")[^>]*?>").matcher(txt);
while (mat.find()){
String res = txt.substring(mat.start());
Matcher m = Pattern.compile(args[0]).matcher(res);
if(m.find(args[0].length()+1)){
if (res.charAt(m.start()-1)=='/') System.out.println(res.substring(0,m.end()+1));
else {
if(m.find())if (m.find())
System.out.println(res.substring(0,m.end()+1));
}
}
}