Ant脚本 - replaceregexp与非匹配组

问题描述:

我试图用replaceregexp搜索和替换多个文件,但专门从谁在2000年范围内的日期的那些版权符号,并乱码,所以我决定用一个非捕获组,用于匹配版权符号后面的数字,并仅替换版权符号。 但由于某些原因,它仍然会取代符号和数字。Ant脚本 - 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> 

我使用的正则表达式是:

(?:\s*)(©|�|©)(?:\s*20\d\d,\sMail Services) 

的非捕获组仍在消耗,在这个意义上的字符由该组中的模式匹配所取代,它只是不“捕获”不会为你储存。 (见这个答案:Regex non-capturing group is capturing

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

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

+0

完美的作品!是没有必要的,但逗号替换字符串是\ 1C \ 3,我相信。 – ROOTKILL