在Ant中,我有一个名为' some_property' 的属性,让我们说它的值是" hello".
我正在尝试使用此属性的值(" hello ")替换文本文件中的占位符作为大写.
所以,我有这个任务:
<replaceregexp match="SOME_PLACE_HOLDER" replace="${some_property}" byline="true">
Run Code Online (Sandbox Code Playgroud)
我希望它能像我有这个任务一样工作:
<replaceregexp match="SOME_PLACE_HOLDER" replace="HELLO" byline="true">
Run Code Online (Sandbox Code Playgroud)
我希望避免使用外部Ant任务(例如Ant-Contrib),因此解决方案需要是一个纯正的正则表达式 - 它必须是可能的!
大写,小写和大写.
谁知道正确的正则表达式?
mar*_*ton 34
我知道你想要避免使用Ant扩展,但是使用正则表达式实现解决方案的约束有点紧张 - 如果下面的规则过多(中断?),则会道歉.
Ant最近附带了一个javascript引擎,所以在Ant xml中实现的任何问题通常都可以隐藏在一个scriptdef.以下是四个案例更改.
在您的情况下,您将获取您的some_property属性并通过upper脚本处理它以获取要在replaceregexp任务中使用的字符串的大写版本.
<scriptdef language="javascript" name="upper">
<attribute name="string" />
<attribute name="to" />
project.setProperty( attributes.get( "to" ),
attributes.get( "string" ).toUpperCase() );
</scriptdef>
<scriptdef language="javascript" name="lower">
<attribute name="string" />
<attribute name="to" />
project.setProperty( attributes.get( "to" ),
attributes.get( "string" ).toLowerCase() );
</scriptdef>
<scriptdef language="javascript" name="ucfirst">
<attribute name="string" />
<attribute name="to" />
var the_string = attributes.get( "string" );
project.setProperty( attributes.get( "to" ),
the_string.substr(0,1).toUpperCase() + the_string.substr(1) );
</scriptdef>
<scriptdef language="javascript" name="capitalize">
<attribute name="string" />
<attribute name="to" />
var s = new String( attributes.get( "string" ) );
project.setProperty( attributes.get( "to" ),
s.toLowerCase().replace( /^.|\s\S/g,
function(a) { return a.toUpperCase(); }) );
</scriptdef>
Run Code Online (Sandbox Code Playgroud)
使用示例:
<property name="phrase" value="the quick brown FOX jUmped oVer the laZy DOG" />
<upper string="${phrase}" to="upper" />
<lower string="${phrase}" to="lower" />
<ucfirst string="${phrase}" to="ucfirst" />
<capitalize string="${phrase}" to="capitalize" />
<echo message="upper( ${phrase} )${line.separator}= '${upper}'" />
<echo message="lower( ${phrase} )${line.separator}= '${lower}'" />
<echo message="ucfirst( ${phrase} )${line.separator}= '${ucfirst}'" />
<echo message="capitalize( ${phrase} )${line.separator}= '${capitalize}'" />
Run Code Online (Sandbox Code Playgroud)
并输出:
[echo] upper( the quick brown FOX jUmped oVer the laZy DOG )
[echo] = 'THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG'
[echo] lower( the quick brown FOX jUmped oVer the laZy DOG )
[echo] = 'the quick brown fox jumped over the lazy dog'
[echo] ucfirst( the quick brown FOX jUmped oVer the laZy DOG )
[echo] = 'The quick brown FOX jUmped oVer the laZy DOG'
[echo] capitalize( the quick brown FOX jUmped oVer the laZy DOG )
[echo] = 'The Quick Brown Fox Jumped Over The Lazy Dog'
Run Code Online (Sandbox Code Playgroud)
感谢Poni和Marco Demaio 实施资本化.