Ant regex compare in if condition

20,046

Solution 1

If you are using 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"/>

Solution 2

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

Check for examples in this page: Ant Manual: Condition Tasks

Also, a example:

<if>
    <contains string="a sample string" substring="sample" />
    <then>
        <echo>match</echo>
    </then>
    <else>
        <echo>not match</echo>
    </else>
</if>
Share:
20,046
Mohamed Saligh
Author by

Mohamed Saligh

Updated on January 12, 2020

Comments

  • Mohamed Saligh
    Mohamed Saligh over 4 years

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