首页 > 解决方案 > 在 C 文件中查找包含单词的字符串定义

问题描述

我需要在包含某个单词的 C 项目中找到所有字符串定义。

例如给定一个名为的文件test.c

int main(int argc, char** argv)
{
  int hello = 0;
  string str = "hello world";
  printf(str); 

  return 0;
}

预期的:

print(check_code_for_word_in_str('test.c','hello'))
>>> ./test.c:4: string str = "hello World";

如果还显示找到字符串的行号会很好。hello = 0请注意如何不打印变量。

我用解析代码检查了类似的东西。但不知道如何处理代码。atspythonc

标签: python

解决方案


尝试执行以下操作:

grep -nE "\"[^\"]*hello[^\"]*\"" *.c

您的情况下的输出是:

test.c:4:  string str = "hello world";

它查找单词“hello”出现在字符串开头之后但字符串关闭之前的位置。它可能并不完美,但对于您的用例来说可能已经足够了。


推荐阅读