日期范围的正则表达式
如果我有这样的目录结构,
yyyy/dd/mm/<files>
有没有办法使用正则表达式在给定时间范围内的所有文件中查找字符串?例如,我有一个时间范围:2010/12/25 - 2011/01/01,我需要 grep 与从 12 月 25 日到 1 月 1 日的日期相对应的目录中的所有文件
如果我以编程方式执行此操作,迭代是否更好在每个 yyyy/dd/mm 目录中的日期范围和 grep 文件中,而不是使用正则表达式来执行此操作?或者不会有什么不同吗?
If I have a directory structure like this
yyyy/dd/mm/<files>
Is there a way to grep for a string in all files in a given time frame using a regex? For example, I have a time frame: 2010/12/25 - 2011/01/01, I need to grep all files in directories corresponding to dates from 25th december to jan 1st
If I am doing this programmatically, is it better to iterate over the date range and grep files in each yyyy/dd/mm directory than to use a regex to do this? Or would it not make a difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于您的情况,它很简单:
将匹配包含您指定范围内的日期的字符串。但一般来说,正则表达式不太适合匹配日期范围。这总是有可能的,但很少是好的。
例如,对于范围 2003/04/25-2011/04/04,您会得到
如果我必须执行类似的操作(并且无法使用文件属性中的创建日期),我会使用 RegexMagic (创建日期范围正则表达式)和 PowerGREP(执行 grep)(如果它是一次性作业),但这些仅在 Windows 上可用。如果我必须更频繁地这样做,我会编写一个小的 Python 脚本来遍历目录树,解析每个目录的日期,检查它是否在范围内,然后查看该目录中的文件。
In your case, it's simple enough:
will match a string that contains a date in the range you specified. But generally, regexes are not a good fit for matching date ranges. It's always a possibility, but rarely a good one.
For example, for the range 2003/04/25-2011/04/04, you get
If I had to do something like this (and couldn't use the creation dates in the file attributes), I would either use RegexMagic (to create the date range regex) and PowerGREP (to do the grepping) if it's a one-time job, but these are only available on Windows. If I had to do this more often, I'd write a small Python script that walks through my directory tree, parses the date for each directory, checks if it's in range, and then looks at the files in that directory.