CMake:在正则表达式中转义变量内的符号 [英] CMake: escaping symbols inside a variable, in regular expressions

查看:175
本文介绍了CMake:在正则表达式中转义变量内的符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用CMake识别带有正则表达式的flexc ++程序的版本。

I try to recognize the version of flexc++ program with regular expressions in CMake.

flexc ++版本的输出类似于:

The ouput of flexc++ version is something like:

prompt$ flexc++ --version
flexc++ V1.01.00

我尝试使用正则表达式提取版本。可执行文件的名称在一个变量中(该变量是其他命令的输出)。问题是flexc ++名称中的字符串 ++。该字符串与符号 +产生冲突(一个或多个匹配项)。小型测试:

I try to extract the version with regular expressions. The name of the executable is in a variable (and this variable is the output of other command). The problem is the string "++" in the flexc++ name. This string create a conflict with the symbol "+" (one or more matchs). A mini test:

set(sample "flexc++ V1.01.00")
set(flexname "flexc++")

string(REGEX REPLACE "^${flexname} V([0-9.]+)$" "\\1"
       output "${sample}")

message("${output}")

抛出下一个错误:

RegularExpression::compile(): Nested *?+.
RegularExpression::compile(): Error in compile.
CMake Error at prueba.cmake:4 (string):
  string sub-command REGEX, mode REPLACE failed to compile regex "^flexc++
  V([0-9.]+)$".

如果我删除了样本和文件名变量中的 ++字符串,它将完美识别版本:

If I erase the "++" string in sample and filename variables, it recognize perfect the version:

set(sample "flexc V1.01.00")
set(flexname "flexc")

string(REGEX REPLACE "^${flexname} V([0-9.]+)$" "\\1"
       output "${sample}")

message("${output}")

输出:

1.01.00

问题是 ++字符串。

如何避免此问题?例如,CMake中是否有任何命令,例如:

How can I avoid this problem? For example, are there in CMake any command like:

scape(flexname_scaped ${flexname})

表现

flexname_scaped <-- flexc\\+\\+

如何解决此问题?

推荐答案

您可以使用< a href = http://www.cmake.org/cmake/help/v2.8.10/cmake.html#command%3astring rel = nofollow> string(REPLACE ...)

You can escape the "++" using string(REPLACE...):

string(REPLACE "++" "\\+\\+" flexname_escaped ${flexname})
string(REGEX REPLACE "^${flexname_escaped} V([0-9.]+)$" "\\1"
       output "${sample}")

这篇关于CMake:在正则表达式中转义变量内的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆