[英]How to get the index by regular expression in ANT
I have a string with a version as .v_september (every month it will vary). 我有一个字符串,其版本为.v_september (每个月都会有所不同)。 In this i wanted to take the value after underscore, which means "sep" (First 3 letters alone).
在此,我想在下划线之后取值,这意味着“ sep” (仅前3个字母)。
By using the regex .v_(.*) i am able to take the complete month and not able to get the first 3 letters alone. 通过使用正则表达式.v _(。*),我可以使用完整的月份,而不能仅获得前3个字母。
Can someone help me out how can I achieve this in Apache ANT. 有人可以帮助我如何在Apache ANT中实现这一目标。 Thanks !
谢谢 !
Regex functions on properties are a bit awkward in native Ant (as opposed to working with text within files). 正则表达式的属性函数在本机Ant中有点尴尬(与在文件中使用文本相反)。 Ant-contrib has the
replaceregexp
task, but I try to avoid ant-contrib whenever possible. Ant-contrib具有
replaceregexp
任务,但我尝试尽可能避免使用ant-contrib。
Instead, it can be accomplished with the loadfile
task and a nested filter: 而是可以通过
loadfile
任务和嵌套过滤器来完成:
<property name="version" value=".v_september" />
<loadfile property="version.month.short">
<propertyresource name="version" />
<filterchain>
<tokenfilter>
<replaceregex pattern="\.v_(.{3}).*" replace="\1" />
</tokenfilter>
</filterchain>
</loadfile>
<echo message="${version.month.short}" />
Regarding the regex pattern, note how it needs to end with .*
. 关于正则表达式模式,请注意它需要如何以
.*
结尾。 This is because Ant doesn't have a "match" function that simply returns the content of a capture group. 这是因为Ant没有简单地返回捕获组内容的“匹配”功能。 It's just running a replacement, so we need to replace everything in the string that isn't part of the group.
它只是在进行替换,因此我们需要替换字符串中不属于该组的所有内容。
.*
will capture everything and for limiting to capturing only three characters you need to write {3}
instead of *
. .*
将捕获所有内容,并且为了仅捕获三个字符,您需要编写{3}
而不是*
。 Also you should escape the .
你也应该逃脱
.
in the beginning of your regex to only match a literal dot. 在正则表达式的开头只能匹配文字点。 You can use this regex and capture from group1,
您可以使用此正则表达式并从group1捕获,
\.v_(.{3})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.