使用 ant,查找与正则表达式匹配的文件并搜索文件中是否存在子字符串 [英] Using ant, find files matching a regular expression and search if a substring in present in the file

查看:54
本文介绍了使用 ant,查找与正则表达式匹配的文件并搜索文件中是否存在子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 2 个文件的目录:

I have a directory containing 2 files:

abc_2010-14-12.log

abc_2010-14-12.log

abcdef.log

我想在第一个文件中搜索子字符串.但我不知道确切的文件名,因为它附加了时间戳.我想我将不得不在文件名中使用正则表达式.

I want to search for a substring in the 1st file. But i dont know the exact file name as it has timestamp appended to it. I think i will have to use regular expression in the file name.

推荐答案

首先你必须确定文件.您可以使用普通的 fileset 包括:

First you must identify the file. You may be able to do that using a normal fileset include:

<fileset dir="." id="the.file" includes="abc_????-??-??.log" />

如果这可能会让您对诸如abc_XXXX-YY-ZZ.log"之类的内容产生误报,那么您可以使用 filename 选择器,它具有比包含和排除规则中可用的glob"通配符更强大的匹配:

If that might give you a false positive on something like 'abc_XXXX-YY-ZZ.log', then you can use a filename selector, which has more powerful matching than the 'glob' wildcarding available in include and exclude rules:

<fileset dir="." id="the.file">
    <filename regex=".*_[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].log"/>
</fileset>

假设(根据您的示例)YYYY-DD-MM,您可能可以进一步限制:

You might be able to restrict further, assuming (based on your example) YYYY-DD-MM:

<filename regex=".*_20[01][0-9]-[0-3][0-9]-[01][0-9].log"/>

这些将为您提供匹配的文件集,希望只是一个文件.

These will give you a fileset that matches, hopefully just the one file.

如果您有多个匹配的文件 - 比如说几天的日志文件 - 您需要决定需要哪一个.您可以使用 tstamp 任务来准确指定基于当前日期,而不是使用模式匹配或 glob.此示例获取与您的格式匹配的昨天日期的文件:

If you have more than one matching file present - say several days' log files - you'll need to decide which one you want. You could make use of the tstamp task to exactly specify the filename based on the current date, rather than using a pattern match or glob. This example gets the file with yesterday's date, matching your format:

<tstamp>
    <format property="file.date" pattern="yyyy-dd-MM"
            offset="-1" unit="day"/>
</tstamp>
<fileset dir="." id="the.file" includes="abc_${file.date}.log" />

确定文件后,您可以使用resourcecontains如果文件匹配,则设置属性的条件 - 在这种情况下,我们搜索查找我":

Once you have identified the file, you can use the resourcecontains condition to set a property if the file matches - in this case we search for 'look for me':

<condition property="file.matches">
    <resourcecontains refid="the.file" substring="look for me" />
</condition>

这篇关于使用 ant,查找与正则表达式匹配的文件并搜索文件中是否存在子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆