linux查找名称中带有可选字符的文件 [英] linux find files with optional character in their name
问题描述
假设我有两个文件:ac
和abc
.我想找到一个正则表达式来匹配两个文件.通常,我希望以下正则表达式能够正常工作,但它永远不会起作用:
suppose I have two files: ac
and abc
. I want to find a regex to match both files. Normally I would expect the following regex to work, but it never does:
find ./ -name ab?c
我已经尝试过转义或不转问问号,这似乎从来没有奏效.通常在我找到的正则表达式文档中; ?
表示:前一个字符重复0或1次,但是find
似乎不理解这一点.
I have tried escaping or not the questionmark, this never seems to work. Normally in the regex documentations I have found; ?
means: previous character repeated 0 or 1 times, but find
doesn't seem to understand this.
我已经在两个不同的查找版本上进行了尝试:GNU查找版本4.2.31和 查找(GNU findutils)4.6.0
I have tried this on two different find versions: GNU find version 4.2.31 and find (GNU findutils) 4.6.0
PS:这适用于*
,但是我特别想只匹配一个可选字符.
PS: this works with *
, but I specifically would like to match just one optional character.
find ./ -name a*c
给予
./ac
./abc
推荐答案
传递给-name
的表达式不是正则表达式,而是全局表达式. (单个)全局表达式不能用于您的用例,但是您可以使用-regex
:
The expression passed to -name
is not a regex, it is a glob expression. A (single) glob expression can't be used for your use case but you can use regular expressions using -regex
:
find -regex '.*/ab?c'
顺便说一句,默认的正则表达式语言是Emacs样式,如下所述: https://www.emacswiki. org/emacs/RegularExpression .您可以使用-regextype
更改正则表达式语言.
Btw, the default regular expression language is Emacs Style as explained here : https://www.emacswiki.org/emacs/RegularExpression . You can change the regex language using -regextype
.
这篇关于linux查找名称中带有可选字符的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!