I have the following two patterns to match an html tag name with possible leading spaces. The first pattern where [ ]*
is inside the named group <doubletag>
doesn't work, but the second pattern where [ ]*
is immediately following the tag symbol "<"
works. I don't know why the first doesn't work.
String s = "< pre href = "dajflka" >ld fjalj09u293 ^% </pre>";
Pattern ptr = Pattern.compile("(<(?<doubletag>[ ]*[a-z]+)([ \\d\\s\\w\\W[^>]])*>)(.*)(</\\k<doubletag>[ ]*>)");
Pattern ptr = Pattern.compile("(<[ ]*(?<doubletag>[a-z]+)([ \\d\\s\\w\\W[^>]])*>)(.*)(</\\k<doubletag>[ ]*>)");
Matcher match = ptr.matcher(s);
if(match.find()){
System.out.println("Found");
}