我正在编写一个perl脚本,该脚本从c源文件打印所需的函数体。我已经编写了一个正则表达式来获得函数体的开头,如下所示
(/(void|int)\s*($function_name)\s*\(.*?\)\s*{/s但这只适用于返回void或int(基本类型)的函数,如何才能更改此正则表达式以处理用户定义的数据类型(结构或指针)
发布于 2014-12-21 22:53:59
试一下这个(未测试的!),尽管它确实希望函数从一行的开头开始:
/
^ # Start of line
\s*(?:struct\s+)[a-z0-9_]+ # return type
\s*\** # return type can be a pointer
\s*([a-z0-9_]+) # Function name
\s*\( # Opening parenthesis
(
(?:struct\s+) # Maybe we accept a struct?
\s*[a-z0-9_]+\** # Argument type
\s*(?:[a-z0-9_]+) # Argument name
\s*,? # Comma to separate the arguments
)*
\s*\) # Closing parenthesis
\s*{? # Maybe a {
\s*$ # End of the line
/mi # Close our regex and mark as case insensitive您可以通过删除空格和注释将所有这些内容压缩到一行中。
不过,使用正则表达式解析代码通常很困难,而且这个正则表达式一点也不完美。
https://stackoverflow.com/questions/27590401
复制相似问题