如何在 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 中查看此示例:

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]+

不过,我想知道我的语法是否有问题 {n}。 CMake 支持吗?如果不是,如何用CMake定义具体的重复次数?

我使用的是旧版 CMake (2.8.11.2)。

根据CMake's documentation,它不支持{n}语法。摘自该页面:

The following characters have special meaning in regular expressions:
^         Matches at beginning of input  
$         Matches at end of input  
.         Matches any single character  
[ ]       Matches any character(s) inside the brackets  
[^ ]      Matches any character(s) not inside the brackets
-         Inside brackets, specifies an inclusive range between
          characters on either side e.g. [a-f] is [abcdef]
          To match a literal - using brackets, make it the first
          or the last character e.g. [+*/-] matches basic
          mathematical operators.
*         Matches preceding pattern zero or more times
+         Matches preceding pattern one or more times 

?         Matches preceding pattern zero or once only   
|         Matches a pattern on either side of the |  
()        Saves a matched subexpression, which can be referenced
          in the REGEX REPLACE operation. Additionally it is saved
          by all regular expression-related commands, including
          e.g. if( MATCHES ), in the variables CMAKE_MATCH_(0..9).

似乎没有办法定义特定的重复次数,而不是复制表达式,例如:

[0-9]{5}

会变成

[0-9][0-9][0-9][0-9][0-9]

我们可以通过使用 shell 命令和 execute_process 来解决这个问题。 例如,echogrep 在 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 的跨平台方面。