1 回答
TA贡献1735条经验 获得超5个赞
这将是一个两步过程
第 1 步:提取函数名称和所有参数
第 2 步:从所有参数列表中提取每个参数名称
步骤1:
让我们将此正则表达式^\S+\s+([^(]+)\(([^)]+)*应用于此字符串void Write(int *p,int a, int b, str *v)此测试字符串
^ # start of string
\S+ # one or more occurence of any non space charactcers
# matches `void`
\s+ # one or more occurence of a space character
# matches the space after `void`
([^(]+) # all characters until opening parenthesis
# matches `Write` and capture it
\( # literally matches opening parenthesis
([^)]+) # matches all characters till closing parenthesis is encountered
# matches arguments signature i.e. `int *p,int a, int b, str *v`
* # matches zero or more occurrence of last capturing group
# last capturing group is string between the parenthesis
# so this star handle the corner case when the argument list is empty
更多详情:https ://regex101.com/r/0m1vs9/2
第2步
现在对参数列表 ( int *p,int a, int b, str *v) 应用这个\s*\S+\s+([^,]+),?带有全局修饰符的正则表达式
这种模式匹配逗号之间的文本,所以让我们解释假设相同的模式
\s* # matches zero or more occurrences of a space character
# this will match any spaces after comma e.g. `int b,<space> str`
\S+ # one or more occurrence of non space character
# matches argument type, i.e. `int`
\s+ # one or more occurrence of space characters
# matches the space between argument name and type, e.g. `int<space>b`
([^,]+) # capture all characters till comma
# this matches the actual argument name
# and also matches any spaces after it
,? # zero or one occurrence of a comma
# this ensures that the argument name is immediately followed by a comma
# this also handles the case for the last argument which doesn't have any comma after it
更多详情:https ://regex101.com/r/9ju60l/1
希望有帮助
添加回答
举报