代码之家  ›  专栏  ›  技术社区  ›  Moia

在qmake文件中使用正则表达式

  •  0
  • Moia  · 技术社区  · 5 年前

    我有这个正则表达式在我的cpp文件中检查版本号

    QString parseVersion()
    {
       // https://regex101.com/r/nFMiA0/1
       QRegularExpression re{R"((?:(\d+\.[.\d]*\d+)))"};
       if(auto match = re.match(VERSION); match.hasMatch()){
          return match.captured();
       }
       return "0.0.0";
    }
    

    我想把它移到pro文件里,比如

    # pseudo code
    contains(VERSION, ((?:(\d+\.[.\d]*\d+)))){
       // yada yada
    }
    

    假设 VERSION 用一根绳子 yada-1.2.3

    如何在pro文件中使用regex?

    0 回复  |  直到 5 年前
        1
  •  1
  •   Wiktor Stribiżew    5 年前

    看一看这个 contains documentation :

    可以为参数值指定正则表达式。

    在你的情况下,你可以使用

    contains(OSDISTRO, .*\d+(?:\.\d+)+) {
    ...
    }
    

    .*\d+(?:\.\d+)+

    • .* -尽可能多的零个或多个字符
    • \d+ -一个或多个数字
    • (?:\.\d+)+

    这也意味着,模式必须匹配整个字符串,这就是以前尝试失败的原因。