Apache Ant: Могу ли я найти во всех файлах определенное регулярное выражение, а затем распечатать совпадения в файл? - PullRequest
4 голосов
/ 24 июня 2011

В 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>

Ответы [ 2 ]

2 голосов
/ 24 июня 2011

Вот фрагмент кода, который может помочь, он создаст ZIP-файл, содержащий любые файлы, соответствующие вашему регулярному выражению. Я использую его для проверки своих сборок, чтобы убедиться, что все токены муравьев были заменены.

<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>
1 голос
/ 24 июня 2011

Вы можете сделать это, используя задачу Concat с вложенной FilterChain .

Примерно так:

<concat destFile="your_output_file">
    <fileset dir="WebContent">
        <include name="**/*.html"/>
    </fileset>
    <filterchain>
        <linecontainsregexp>
            <regexp pattern="your_pattern_to_match" />
        </linecontainsregexp>
        <tokenfilter>
            <replaceregex pattern="your_pattern_to_extract" replace="output_required" />
        </tokenfilter>
    </filterchain>
</concat>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...