Ant regex compare in case of condition

I have str1="A sample string" . If str1 contains sample , I need to echo something like match and otherwise not matching . I am not familiar with scripts. Please help me.

+7
source share
2 answers

If you are using a newer ant try ... http://ant.apache.org/manual/Tasks/conditions.html

 <condition property="legal-password"> <matches pattern="[1-9]" string="${user-input}"/> </condition> <fail message="Your password should at least contain one number" unless="legal-password"/> 
+13
source

If you just want to know if a substring exists in a string, you can use the <contains> task along with the <if> task from ant -contrib . If you want to scan a pattern (regular expression) in a string, use <matches> instead of <contains> .

Check out the examples on this page: Ant Manual: Condition Tasks

In addition, an example:

 <if> <contains string="a sample string" substring="sample" /> <then> <echo>match</echo> </then> <else> <echo>not match</echo> </else> </if> 
+10
source

All Articles