正则表达式,如何search或match一个函数体

[复制链接]
查看11 | 回复5 | 2021-1-27 07:00:23 | 显示全部楼层 |阅读模式
比如,我的文件内容如下:
voidA(){
//函数体内容,可能包括一切可能的语法,比如{...},注释等
}

intB(){
//函数体内容,可能包括一切可能的语法,比如{...},注释等
}
intC(){
//函数体内容,可能包括一切可能的语法,比如{...},注释等
}
.....
那么,我如何用正则表达式,找出每个函数(包括函数头和函数体)?
想了半天没找到如何写regex,函数与函数的界限似乎不太明显,似乎匹配第一个“{”和最后一个“}”,是比较准确的方法?谁能帮忙写出提取的正则表达式?


分 -->
回复

使用道具 举报

千问 | 2021-1-27 07:00:23 | 显示全部楼层
对于正则来讲基本上是不可能的任务。
还是搞语法分析来的快一些。
回复

使用道具 举报

千问 | 2021-1-27 07:00:23 | 显示全部楼层
引用1楼akirya的回复:对于正则来讲基本上是不可能的任务。
还是搞语法分析来的快一些。

boost::regex应该是可以实现的,我在官网上找到如下内容,但就是不会写。
RecursiveExpressions
(?N)(?-N)(?+N)(?R)(?0)
(?R)and(?0)recursetothestartoftheentirepattern.
(?N)executessub-expressionNrecursively,forexample(?2)willrecursetosub-expression2.
(?-N)and(?+N)arerelativerecursions,soforexample(?-1)recursestothelastsub-expressiontobedeclared,and(?+1)recursestothenextsub-expressiontobedeclared.
ConditionalExpressions
(?(condition)yes-pattern|no-pattern)attemptstomatchyes-patterniftheconditionistrue,otherwiseattemptstomatchno-pattern.
(?(condition)yes-pattern)attemptstomatchyes-patterniftheconditionistrue,otherwisefails.
conditionmaybeeither:aforwardlookaheadassert,theindexofamarkedsub-expression(theconditionbecomestrueifthesub-expressionhasbeenmatched),oranindexofarecursion(theconditionbecometrueifweareexecutingdirectlyinsidethespecifiedrecursion).
Hereisasummaryofthepossiblepredicates:
(?(?=assert)yes-pattern|no-pattern)Executesyes-patterniftheforwardlook-aheadassertmatches,otherwiseexecutesno-pattern.
(?(?!assert)yes-pattern|no-pattern)Executesyes-patterniftheforwardlook-aheadassertdoesnotmatch,otherwiseexecutesno-pattern.
(?(R)yes-pattern|no-pattern)Executesyes-patternifweareexecutinginsidearecursion,otherwiseexecutesno-pattern.
(?(RN)yes-pattern|no-pattern)Executesyes-patternifweareexecutinginsidearecursiontosub-expressionN,otherwiseexecutesno-pattern.
(?(DEFINE)never-exectuted-pattern)Definesablockofcodethatisneverexecutedandmatchesnocharacters:thisisusuallyusedtodefineoneormorenamedsub-expressionswhicharereferedtofromelsewhereinthepattern.
不知道boost的regex如何写。
回复

使用道具 举报

千问 | 2021-1-27 07:00:23 | 显示全部楼层
""里面包含{}呢?注释里面包含"{}"呢?
回复

使用道具 举报

千问 | 2021-1-27 07:00:23 | 显示全部楼层
引用3楼akirya的回复:""里面包含{}呢?注释里面包含"{}"呢?
只要是成对的,应该是可以判断函数的结尾的。

回复

使用道具 举报

千问 | 2021-1-27 07:00:23 | 显示全部楼层
引用4楼sealdh的回复:Quote: 引用3楼akirya的回复:
""里面包含{}呢?注释里面包含"{}"呢?

只要是成对的,应该是可以判断函数的结尾的。

在""注释中不一定成对啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行