如何在CMake中使用正则表达式的{n}语法 [英] How to use the {n} syntax of regex with CMake
问题描述
我有此字符串 2017-03-05-02-10-10_78205,我想将其与该模式匹配 [0-9] {4}(-[0-9] {2}){5} _ [0-9] +
,但在CMake上不起作用。在CMake中查看此示例:
I have this string "2017-03-05-02-10-10_78205" and I want to match it with this pattern [0-9]{4}(-[0-9]{2}){5}_[0-9]+
but it doesn't work on CMake. See this example in CMake :
set(stuff "2017-03-05-02-10-10_78205")
if( "${stuff}" MATCHES "[0-9]{4}(-[0-9]{2}){5}_[0-9]+")
message("Hello")
endif()
CMake似乎不支持语法 {n}
。显然,我使用该模式 [0-9-] + _ [0-9] +
CMake doesn't seem to support the syntax {n}
. Obviously, I solved my problem with that pattern [0-9-]+_[0-9]+
不过,我想知道语法 {n}
是否做错了。 CMake支持吗?如果不是,如何使用CMake定义特定的重复次数?
Nevertheless, I would like to know if I'm doing something wrong with the syntax {n}
. Is it supported by CMake ? If not, how to define a specific number of repetition with CMake ?
我正在使用旧的CMake版本(2.8.11.2)。
I'm using an old CMake version (2.8.11.2).
推荐答案
我们可以通过使用Shell命令和 execute_process
解决此问题。
例如,在Linux上使用 echo
和 grep
:
We can get around this problem by using shell commands and execute_process
.
For instance, with echo
and grep
on linux :
set(stuff "2017-03-05-02-10-10_78205")
set(regexp "[0-9]{4}(-[0-9]{2}){5}_[0-9]+")
execute_process( COMMAND echo "${stuff}"
COMMAND grep -E -o "${regexp}"
OUTPUT_VARIABLE thing )
if(thing)
message("Hello")
endif()
但是我们放弃了CMake的跨平台方面。
But we loose the cross-platform aspect of CMake.
这篇关于如何在CMake中使用正则表达式的{n}语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!