ANT脚本 - 将replaceregexp替换为不匹配的组

时间:2016-08-23 21:47:05

标签: regex replace ant

我试图使用6.123233995736766e-17来搜索和替换多个文件上的版权符号和垃圾字符,但具体来自那些日期在2000范围内的文件,所以我决定使用非捕获组匹配版权符号后面的数字,并仅替换版权符号。 但由于某种原因,它仍然取代了符号和数字。

例如,我会匹配:

replaceregexp

预期结果将是:

© 2007 Mail Services Bla Bla Bla

但我明白了:

C 2007 Mail Services Bla Bla Bla

这是我的代码:

C Bla Bla Bla

我正在使用的正则表达式是:

<replaceregexp match="${match.exp}" replace="${replace.str}" byline="true">
    <fileset dir="${parent.dir}">
        <include name="**/*.js"/>
        <include name="**/*.jsp"/>
        <include name="**/*.xml"/>
        <include name="**/*.properties"/>
        <include name="**/*.css"/>
        <include name="**/*.java"/>
        <!-- Excluding some files -->
        <exclude name="**/yu.js"/>
        <exclude name="**/JSMenu.js"/>
        <exclude name="**/jqueryTimerPack.js."/>
        <exclude name="**/jqu.js"/>
        <exclude name="**/jqui.js"/>
        <exclude name="**/AM.properties"/>
    </fileset>
</replaceregexp>

1 个答案:

答案 0 :(得分:0)

非捕获组仍然被消耗和替换,它只是没有被捕获&#34;从某种意义上说,与该组中的模式匹配的字符不会为您存储。 (见这个答案:Regex non-capturing group is capturing

相反,您应该捕获组并使用它们来重建所需的字符串。

例如,如果您的正则表达式为(?:\s*)(©|�|©)(?:\s*20\d\d,\sMail Services),请尝试使用(\s*)(©|�|©)(\s*20\d\d,\sMail Services)和替换\1C\2之类的字符串。我不确定最后一组中的逗号是否有意,因为它与您提供的示例不匹配。