查找带有"regex"匹配项的命令不起作用 [英] find command with 'regex' match not working

查看:86
本文介绍了查找带有"regex"匹配项的命令不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试与此正则表达式下面的文件进行简单的文件匹配,我测试过可以从此 page 为示例文件名ABC_YYYYMMDDHHMMSS.sha1

Am trying to do a simple file-name match with this below regex which I tested to be working from this page for a sample file-name ABC_YYYYMMDDHHMMSS.sha1

ABC_20[0-9]{2}(0[1-9]|1[0-2])([0-2][0-9]|3[0-1])([0-2][0-3])([0-5][0-9])([0-5][0-9])\.sha1

当我在find-regex标志中将其耦合时,例如

When I couple this in the -regex flag of find like

find . -type f -regex "ABC_20[0-9]{2}(0[1-9]|1[0-2])([0-2][0-9]|3[0-1])([0-2][0-3])([0-5][0-9])([0-5][0-9])\.sha1"

该命令无法识别路径中存在的文件(例如ABC_20161231225950.sha1).从此 page ,但我意识到我的类型是posix-extended并尝试如下操作,

the command is not identifying the file present in the path (e.g ABC_20161231225950.sha1). Am aware of many existing regex-types from this page, but I realized my type is posix-extended and tried as below,

find . -type f -regextype posix-extended -regex 'ABC_20[0-9]{2}(0[1-9]|1[0-2])([0-2][0-9]|3[0-1])([0-2][0-3])([0-5][0-9])([0-5][0-9])\.sha1'

,仍然没有结果.我搜索了一些类似的此类问题,但它们涉及提供错误的正则表达式,导致找不到文件.就我而言,尽管发现正则表达式是正确的.我需要知道我在这里想念的是什么.在find中使用-regex时,也可​​能如何调试不匹配的问题.

and still no result. I searched around some similar questions of this type, but they were involving giving the wrong regex leading to files not being found. In my case, though the regex is found to be proper. I need to know what am I missing here. Also possibly how to debug non matching issues when using -regex in find.

注意:-我可以对正则表达式中的捕获组进行一些优化,但这不在当前问题的范围之内.

Note:- I could do some optimizations over the capturing groups in the regex, but that is not in the scope of the current question.

推荐答案

在正则表达式开始处添加.*,因为在路径开始处始终会得到./之类的内容

add .* at the start of your regex because you will always get something like ./ at start of path

find . -type f -regextype posix-extended -regex '.*ABC_20[0-9]{2}(0[1-9]|1[0-2])([0-2][0-9]|3[0-1])([0-2][0-3])([0-5][0-9])([0-5][0-9])\.sha1'

这篇关于查找带有"regex"匹配项的命令不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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