使用Ant,查找文件匹配正前pression和搜索如果present一个子文件中 [英] Using ant, find files matching a regular expression and search if a substring in present in the file
问题描述
我有一个包含2个文件目录:
I have a directory containing 2 files:
abc_2010-14-12.log
abc_2010-14-12.log
abcdef.log
abcdef.log
我要搜索的第一个文件中的字符串。但我不知道确切的文件名,因为它已经时间戳追加到它。我想我会在文件名中使用常规的前pression。
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.
推荐答案
首先,你必须确定文件。您可能能够做到这一点使用的是正常的文件集
包括:
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',那么你可以使用的 文件名
选择,它在包括和排除规则提供更强大的匹配比'水珠'通配符:
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-MM-DD:
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.
如果您有多个匹配的文件present - 说好几天的日志文件 - 你需要决定你想要哪一个。你可以利用 TSTAMP
的任务准确指定基于当前日期的文件名,而不是使用图案匹配或水珠。这个例子获取与昨天的日期的文件,符合格式为:
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,查找文件匹配正前pression和搜索如果present一个子文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!