背景
- 想要一块代码中抽取出 函数返回值, 函数名 函数类型,用来编出 main 函数
想要的功能,这能用正则做吗?
- 可以匹配 c++函数的 返回值类型、函数名、参数类型列表
- 返回值类型、参数类型会有:
- 类型 1:
- int
- char
- string
- TreeNode *
- Node *
- vector<类型 1>
- vector<类型 1> &
- vector<vector<类型 1>>
- vector<vector<类型 1>> &
问了 OpenAI 感觉不太对,每次问结果都不一样
Here's a regular expression that matches the return value type, function name, and parameter type in C++: (int|vector<int>|vector<vector<int>>|string|vector<string>|vector<vector<string>>|ListNode\*|vector<ListNode\*>|char|vector<char>|vector<vector<char>>|TreeNode\*|Node\*)\s(\w+)\((.*)\) The regular expression uses the following components: (int|vector<int>|vector<vector<int>>|string|vector<string>|vector<vector<string>>|ListNode\*|vector<ListNode\*>|char|vector<char>|vector<vector<char>>|TreeNode\*|Node\*) matches the return value type, which can be one of the specified types. \s matches a whitespace character. (\w+) matches the function name, which is one or more word characters. \(.*\) matches the parameter list, which is zero or more characters enclosed in parentheses.
举个例子
vector<int> f(vector<int>& a, TreeNode *b, int d){ }
vector<int> arg1 = {}; TreeNode * arg2 = new TreeNode(xxx); int arg3 = 0; vector<int> result = f(arg1, arg2, arg3);
如果不能做,那只能写代码硬匹配了?