python - 在 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
请注意如何不打印变量。
我用解析代码检查了类似的东西。但不知道如何处理代码。ats
python
c
解决方案
尝试执行以下操作:
grep -nE "\"[^\"]*hello[^\"]*\"" *.c
您的情况下的输出是:
test.c:4: string str = "hello world";
它查找单词“hello”出现在字符串开头之后但字符串关闭之前的位置。它可能并不完美,但对于您的用例来说可能已经足够了。
推荐阅读
- javascript - Linux 上的 Visual Studio Code 中用于语法突出显示的默认 html.json 文件在哪里?
- c# - 如何将新的 WebView2 WinForms 控件添加到 .NET Core 的工具箱中?
- php - 将此 curl 命令转换为 php curl
- google-bigquery - Bigquery 表嵌套多值列在查询时出错
- php - 如果 x 不包含 y | 是否有向 x 添加文本的功能?PHP
- solr - Apache solr 索引文件 (pdf,docx,..) 通过 ftp
- selenium - 初始化错误:java.lang.NoClassDefFoundError:io/cucumber/stepexpression/TypeRegistry
- angular - Angular ngbTypeahead 不会重置模糊时的输入值
- java - 嵌入式事务管理 JPA
- angular7 - PrimeNg - P-Dropdown 不加载选项和样式,不工作