V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ccagml
V2EX  ›  正则表达式

想请教一个正则表达式,这正则能做吗

  •  
  •   ccagml · 2023-02-10 17:44:49 +08:00 · 588 次点击
    这是一个创建于 412 天前的主题,其中的信息可能已经有所发展或是发生改变。

    背景

    • 想要一块代码中抽取出 函数返回值, 函数名 函数类型,用来编出 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);
    

    如果不能做,那只能写代码硬匹配了?

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3066 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:35 · PVG 22:35 · LAX 07:35 · JFK 10:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.