gpt4 book ai didi

regex - 大写,小写,大写 Ant 属性

转载 作者:行者123 更新时间:2023-12-03 12:35:27 26 4
gpt4 key购买 nike

在 Ant 中,我有一个名为 ' some_property 的属性。 ',假设它的值为“hello”。

我正在尝试 替换文本文件中的占位符 将此属性的值(“ hello ”)作为 大写 .
所以,我有这个任务:

<replaceregexp match="SOME_PLACE_HOLDER" replace="${some_property}" byline="true">

我希望它像我有这个任务一样工作:
<replaceregexp match="SOME_PLACE_HOLDER" replace="HELLO" byline="true">

我希望避免外部 Ant 任务(例如 Ant-Contrib),因此 解决方案必须是纯正则表达式 - 这一定是可能的!

大写、小写和大写。

有人知道正确的正则表达式吗?

最佳答案

我知道您想避免 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>

示例使用:
<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}'" />

并输出:
[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'

感谢 Poni 和 Marco Demaio 提供 implementation of the Capitalization .

关于regex - 大写,小写,大写 Ant 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7129672/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com