f9EHtxE2UtX3fG3

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

  •  
  •   f9EHtxE2UtX3fG3 · Feb 10, 2023 · 3188 views
    This topic created in 1201 days ago, the information mentioned may be changed or developed.

    背景

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

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

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5234 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 01:22 · PVG 09:22 · LAX 18:22 · JFK 21:22
    ♥ Do have faith in what you're doing.