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

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

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

    腾讯云最新优惠活动来了:云产品限时1折,云服务器低至88元/年 ,点击这里立即抢购:9i0i.cn/qcloud,更有2860元代金券免费领取,付款直接抵现金用,点击这里立即领取:9i0i.cn/qcloudquan

    (福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

    背景

    • 想要一块代码中抽取出 函数返回值, 函数名 函数类型,用来编出 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   ·   我们的愿景   ·   实用小工具   ·   3076 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 13:20 · PVG 21:20 · LAX 06:20 · JFK 09:20
    Developed with CodeLauncher
    ? Do have faith in what you're doing.


    http://www.vxiaotou.com