Regex grep,在函数()中查找$tring和$\tring

2024-06-28 16:20:28 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的debian机器上,我在玩一些代码和regex。总之,我希望在一些代码文件中找到以下模式:

function(some text $tring||$_string)

目前我正在使用以下命令

find . -type f | xargs grep "function"

我试过用

find . -type f | xargs grep "function(*$string||$_tring*)"

但没有起作用。你知道吗

本质上,它需要搜索所有符合以下规则的模式

  • 包含函数()
  • 在函数内部,匹配以下字符串之一
    • $string或
    • $\字符串

我该怎么做?你知道吗


Tags: 文件函数字符串代码机器stringtype模式
3条回答

我建议您ack,它是为此目的而设计的(它将文件过滤为不在.svn.git等目录中搜索)。你知道吗

http://betterthangrep.com/

所以:

ack -r "function.*(\$[s_]tring)"

试试看

find . -type f | xargs egrep 'function\(.*$[s_]tring.*\)'
find . -type f -exec egrep -Hn 'function\(.*(\$string|\$_string).*\)' '{}' ';'

能够处理包含空格的文件名,并且如果有很多文件。你知道吗

man 3 regex如果您想了解有关regex语法的更多信息:

.            any character, in shell (sh/csh) this is '?'
.*           zero or more any characters
a*           zero or more a's
(alpha|beta) groups, either alpha or beta
^            beginning of string
$            end of string, that's why it needs to be escaped

相关问题 更多 >