Your code seems to work, but it will also catch non-matching tags, try the following, which will only match matching tags, and case-insensitive.
$line ="<AB>hello</CD>";
$line1="<Hey>Good Day</HEY>";
if($line =~ m'<(\w+)>.*</\1>'i){printf("A1:%s\n",$1);}
if($line1 =~ m'<(\w+)>.*</\1>'i){printf("A2:%s\n",$1);}
if($line =~ m!<[^>]*>(\S+)</[^>]*>!){printf("B1:%s\n",$line);}
if($line1 =~ m!<[^>]*>(\S+)</[^>]*>!){printf("B2:%s\n",$line1);}
A2:Hey
B1:<AB>hello</CD>
Tool completed successfully