gpt4 book ai didi

c - 如何使用正则表达式匹配 C 函数原型(prototype)和变量定义?

转载 作者:行者123 更新时间:2023-11-30 16:00:29 25 4
gpt4 key购买 nike

这个问题之前已经被问过,但我有一个特殊的情况,我应该能够使用正则表达式来处理。

我正在尝试读取 Doxygen 的警告日志,源代码是 C 语言(到目前为止,我不敢考虑 C++)。

我需要匹配该日志中找到的函数和变量定义并选取函数和变量名称。

更具体地说,日志有类似

的行
/home/me/blaa.c:10:Warning: Member a_function(int a, int b) (function) of file blaa.c is not documented

/home/me/blaa.h:10:Warning: Member a_variable[SOME_CONST(sizeof(SOME_STRUCT), 64)*ANOTHER_CONST] (variable) of file blaa.h is not documented

有了 C 语言中可以有的所有变体...

我可以只用一个正则表达式来匹配那些,还是我不应该打扰?括号中的“参数”列表(我松散地使用它也包括变量)后面的单词是一组某些单词(函数、变量、枚举等),因此如果没有其他帮助,我可以与这些单词匹配,但我最好不要,以防日志中存在我尚未看到的类型。

我当前的尝试看起来像

'(?P<full_path>.+):\d+:\s+Warning:\s+Member\s+(?P<member_name>.+)([\(\[](\**)\s*\w+([,)])[\)\]))*\s+\((?P<member_type>.+)\) of file\s+(?P<filename>.+)\s+is not documented'

(我使用Python的re包。)

但它仍然无法捕获所有内容。

编辑:我在上次编辑中犯了一些错误。

最佳答案

您允许 <member_name> 之间有零个或多个匹配项和<member_type> 。试试这个:

'(?P<full_path>.+):\d+:\s+Warning:\s+Member\s+(?P<member_name>\w+).*\s+\((?P<member_type>\w+)\) of file\s+(?P<filename>.+)\s+is not documented'

关于c - 如何使用正则表达式匹配 C 函数原型(prototype)和变量定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7726703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com