我试图使用replaceregexp来搜索和替换多个文件上的版权符号和垃圾字符,但特别是那些日期在2000范围内的文件,因此我决定使用非捕获组来匹配版权符号后面的数字,并只替换版权符号。但出于某种原因,它仍然替换了符号和数字。
例如,我会匹配:
© 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)发布于 2016-08-24 05:58:54
未捕获的组仍然被使用和替换,只是在与该组中的模式匹配的字符不会为您存储的意义上,它不会被“捕获”。(请参阅此答案:Regex non-capturing group is capturing)
相反,您应该捕获这些组,并使用它们重新构建所需的字符串。
例如,如果您的正则表达式是(?:\s*)(©|�|©)(?:\s*20\d\d,\sMail Services),请尝试使用(\s*)(©|�|©)(\s*20\d\d,\sMail Services)和类似于\1C\2的替换字符串。我不确定最后一组中的逗号是否是故意的,因为它与您提供的示例不匹配。
https://stackoverflow.com/questions/39111204
复制相似问题