2012-11-26 26 views
0

我嘗試使用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\\+\\+ 

我該如何解決這個問題?

回答

3

你能逃過 「++」 使用string(REPLACE...)

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