Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用regex参数从变量中查找_Regex_Bash_Find - Fatal编程技术网

使用regex参数从变量中查找

使用regex参数从变量中查找,regex,bash,find,Regex,Bash,Find,我想创建使用以下方式查找的脚本: find . -regex $1 | while read prom; do echo $prom done 我想从option获取正则表达式,但我无法使其工作。我试着直接使用一些正则表达式(f.e.“*(txt)§”)而不是1美元,但没用。关于使用find,我忘记了什么?有什么理由在那里使用括号吗?尝试将*txt$作为您的正则表达式(在您的问题中没有那么有趣的“section symbol”符号) 如果确实需要括号(作为捕获括号?),则必须执行*\(

我想创建使用以下方式查找的脚本:

find . -regex $1 | while read prom; do
    echo $prom
done

我想从option获取正则表达式,但我无法使其工作。我试着直接使用一些正则表达式(f.e.“*(txt)§”)而不是1美元,但没用。关于使用find,我忘记了什么?

有什么理由在那里使用括号吗?尝试将
*txt$
作为您的正则表达式(在您的问题中没有那么有趣的“section symbol”符号)

如果确实需要括号(作为捕获括号?),则必须执行
*\(txt\)$
,因为
find
的默认正则表达式类型是Emacs样式,其中
()
是文本,必须转义才能按正则表达式进行解释

您还可以执行
find-regextype posix extended-regex'.*(txt)$”
,注意将regex更改为extended posix regex的
-regextype posix extended
,其中
()
是特殊字符(
查找-regextype asdf
通常会给您一条错误消息,列出您可以为
regextype
输入的所有选项)

此外,在bash脚本中,应该将
$1
括在引号中:

find . -regex "$1"

(除非
$1
已经加上了引号,在这种情况下,请忽略该建议)。

我的示例看起来应该是
*\(txt\)$
,但我不知道在文本中我必须使用\\(该符号在处理时存在问题)。你的回答用引号帮助我。Thank“(除非$1已经被输入并在其周围加上引号,在这种情况下,忽略该建议)”--不,它在传递到脚本/函数时如何被引用并不重要:当您“取消引用”变量时,请使用引号。