regex - Apache Ant : Can I search all files for a specific regex and then print the matches to a file?

标签 regex ant build

在 Ant build.xml 中,我希望能够使用以下正则表达式在所有 .html 文件中找到任何匹配项:

("|')((?!http://|#|mailto:|&|/)([^#\n\s\."])+?\.([^#\n\s"])+?)\1

然后,我想在文件中列出\2 的那些匹配项。这可能吗?

最终结果,感谢@bakoyaro:
    <echo message="Collecting appcache files" />
    <concat destFile="your_output_file">
        <fileset dir="./${dir.publish}">
            <include name="**/*.html"/>
        </fileset>
        <filterchain>
            <linecontainsregexp>
                <regexp pattern="(.)*?(&quot;|')((?!http://|\?|#|mailto:|\1)([^#\n\s\.&quot;'?])+?\.([^#\n\s&quot;'?])+?)\2" />
            </linecontainsregexp>
            <tokenfilter>
                <replaceregex pattern="(.)*?(&quot;|')((?!http://|\?|#|mailto:|\1)([^#\n\s\.&quot;'?])+?\.([^#\n\s&quot;'?])+?)\2" flags="g" replace="\1\2\3\2${line.separator}" />
            </tokenfilter>
            <linecontainsregexp>
                <regexp pattern="(.)*?(&quot;|')((?!http://|\?|#|mailto:|\1)([^#\n\s\.&quot;'?])+?\.([^#\n\s&quot;'?])+?)\2" />
            </linecontainsregexp>
            <tokenfilter>
                <replaceregex pattern="(.)*?(&quot;|')((?!http://|\?|#|mailto:|\1)([^#\n\s\.&quot;'?])+?\.([^#\n\s&quot;'?])+?)\2" flags="g" replace="\3" />
            </tokenfilter>
            <linecontainsregexp>
                <regexp pattern="((?!http://|\?|#|mailto:|\1)([^#\n\s\.&quot;'?])+?\.([^#\n\s&quot;'?])+?)" />
            </linecontainsregexp>
        </filterchain>
    </concat>

最佳答案

这是一个可能有帮助的片段,它将创建一个 .zip 文件,其中包含与您的正则表达式匹配的任何文件。我用它来检查我的构建以确保所有 ant 标记都被替换。

<zip destfile="${your_file_name}" update="true" whenempty="skip">
            <fileset dir="${your_search_directory}">
                    <!-- your file pattern -->
                    <include name="**/*.html" />
                    <!-- this will destroy an executable file. best to exclude them -->
                <exclude name="**/*.jar" />
                <containsregexp expression="your_regex_to_match" />
            </fileset>
        </zip>

关于regex - Apache Ant : Can I search all files for a specific regex and then print the matches to a file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6466476/

相关文章:

java - 将字符与正则表达式匹配

java简单的正则表达式

html - 从字符串中提取 HTML 标签名称

java - 替换字符串中的\a25之类的字符串

java - 有关所使用的 JUnit jar 版本的 Ant JUnit 任务报告(例如 Junit 3、3.8、4)

java - 将ant源代码构建的库添加到我的eclipse项目java中?

java - 如何让 ant 不打印 BuildException/ExitStatusException 堆栈跟踪

jenkins - 如何使用单个作业配置在多个从站上运行 Jenkins 作业?

angularjs - Grunt CDNify 不会对文件进行 CNDify

javascript - 如何避免 "Forcing rebuild: JavaScript files need to be re-encrypted"