如何用正则表达式匹配到C语言中的函数实现部分的函数头部分。
比如
1)
[code="c"]
int test1(int a, int b){
int a;
int b;
}
[/code]
匹配到
[code="c"]int test1(int a, int b){[/code]
2)
[code="c"]
int test1(int a, int b)
{
int a;
int b;
}
[/code]
匹配到
[code="c"]
int test1(int a, int b)
{
[/code]
3)
[code="c"]
int test1(int a,
int b){
int a;
int b;
}
[/code]
匹配到
[code="c"]
int test1(int a,
int b){
[/code]
这个正则表达式该如何写?
碰巧前几天写了点代码生成的脚本,以下为一些摘录
虽然不是很完美,但是大部分情况都能处理
要完美解析的话,请找yacc
1.预处理(所有行)
删除所有块注释: gsub!(/\/*.*?*\//m, '')
删除逗号后面的换行: gsub!(/\,\s*\n+/m,'')
2.处理行末
去掉行末注释: line.sub! /\/\/.*?$/, ''
去掉行末分号和左大括号: line.sub! /\s*[\;{]?\s*$/, ''
3.匹配函数
first_match = (line =~ /([\w:]+)(([^)]*))$/)
4.if first_match then #结果【modifiers type funname(params[0],params[1]...)】
prefix = str[0...first_match]
funname = $1.dup
params = $2 ? ($2.split ',') : []
modifiers = prefix[0...(prefix =~ /(\w+\W*)$/)] || ''
type = $1.dup.gsub ' ',''
..#进一步解析modifier,type,funname,params
一些补充:脚本是ruby,领会精神就行了……
/regexp/m 多行模式正则表达式
/regexp/ 单行模式正则表达式
=~ 若匹配返回第一个匹配的位置,不匹配则返回nil
$1,$2 反向引用(分别对应第1,2个括号)
string[start...end] 截取字符串从start到end-1的那段
dup 复制
sub,gsub,sub!,gsub! 字符串替换,感叹号表示替换自身,没感叹号则返回新串
正则表达式语法全世界都大同小异,就不解释了……