CMake:在正则表达式中转义变量内的符号
问题描述:
我尝试使用CMake中的正则表达式来识别flexC++程序的版本。CMake:在正则表达式中转义变量内的符号
flexc的输出中++版本是一样的东西:
prompt$ flexc++ --version
flexc++ V1.01.00
我尝试用正则表达式来提取版本。可执行文件的名称在一个变量中(而这个变量是其他命令的输出)。问题是flexC++名称中的字符串“++”。该字符串与符号“+”(一个或多个匹配)产生冲突。一个小型测试:
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.]+)$".
如果我抹去 “++” 字符串中的样品和文件名的变量,它认识到完善的版本:
set(sample "flexc V1.01.00")
set(flexname "flexc")
string(REGEX REPLACE "^${flexname} V([0-9.]+)$" "\\1"
output "${sample}")
message("${output}")
输出:
1.01.00
Tha t表示问题是“++”字符串。
我该如何避免这个问题?例如,是否有在CMake的像任何命令:
scape(flexname_scaped ${flexname})
执行
flexname_scaped <-- flexc\\+\\+
?
我该如何解决这个问题?
答
你能逃过 “++” 使用string(REPLACE...)
:
string(REPLACE "++" "\\+\\+" flexname_escaped ${flexname})
string(REGEX REPLACE "^${flexname_escaped} V([0-9.]+)$" "\\1"
output "${sample}")