Ant pattern matcher starting with and having decimal values
Clash Royale CLAN TAG#URR8PPP
Ant pattern matcher starting with and having decimal values
I want to do an ant match for initializing a variable as below."version" is input.
if version is 1.2.X.X then value should be true (X means anything[1-9]) else value should be false
Example :
version = 1.3.0.5, value = false
version = 1.2.0.5, value = true
version = 1.2.5.3, value = true
version = 2.1.0.5, value = false
<if>
<matches string="@VERSION" pattern="^1.2"/>
<then>
<property name="version.value" value="true"/>
</then>
<else>
<property name="version.value" value="false"/>
</else>
</if>
What should be the pattern to check first 2 digit including '.' [1.2.x.x]
2 Answers
2
I would highly recommend avoiding ant-contrib
tasks whenever possible, as they tend to encourage the use of Ant as a programming language, which will often cause problems down the road as your build script evolves. Ant's native condition
task is perfectly suited for this situation.
ant-contrib
condition
Regarding your pattern, as the The fourth bird already posted, .
is a special character in regex so it needs to be escaped.
.
<property name="VERSION" value="1.2.0.5" />
<condition property="version.value" else="false">
<matches string="$VERSION" pattern="^1.2" />
</condition>
<echo message="$version.value" />
A couple notes:
By default, the condition
task sets the property to "true" if the nested condition evaluates to true, but leaves the property unset if it evaluates to false. Normally, this is a useful feature in Ant, but if you really need it to be set to "false" (or any other value), you can use the else
attribute which I've included.
condition
else
The simple pattern ^1.2
technically works here, but it's not very restrictive. If you want to avoid matching values that start with 1.2
but don't conform specifically to a 1.2.x.x
pattern (e.g. 1.2asdf
, you'll want to use something that checks the whole string, such as ^1.2.d.d$
.
^1.2
1.2
1.2.x.x
1.2asdf
^1.2.d.d$
You have to escape the dot to match it literally.
For 1.2.0.5
to match you have to include the zero [0-9]
for the third digit.
1.2.0.5
[0-9]
Try this pattern:
^1.2.[0-9].[0-9]$
^1.2.[0-9].[0-9]$
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.