You may create a macrodef using the builtin (since JDK 6) Javascript engine, no extra libraries needed.
something like that :
<project>
<!--
Loglevel defined in org.apache.tools.ant.Project
MSG_ERR = 0;
MSG_WARN = 1;
MSG_INFO = 2;
MSG_VERBOSE = 3;
MSG_DEBUG = 4;
-->
<!-- macrodef creating a property with name of executing target
optional logging to stdout via adjustable loglevel -->
<macrodef name="showtargetname">
<attribute name="property"/>
<attribute name="loglevel" default="3"/>
<sequential>
<script language="javascript">
t = self.getOwningTarget();
self.getProject().setNewProperty("@{property}", t);
self.log("TargetName => " + t, @{loglevel});
</script>
</sequential>
</macrodef>
<target name="foobar">
<showtargetname property="foo"/>
<echo>$${foo} => ${foo}</echo>
</target>
</project>
Afterwards you have the targetname available as property for further processing, f.e. use condition task (=> contains) to check whether the property includes certain strings.
Alternatively add the logic for " targetname contains certain strings " also to the macrodef.
To explore what is available for self, use :
<script language="javascript">
for(x in self) {self.log(x);}
</script>