如何将regex的{n}语法与CMake一起使用
我有一个字符串“2017-03-05-02-10-1078205”,我想将它与这个模式匹配如何将regex的{n}语法与CMake一起使用,regex,cmake,repeat,Regex,Cmake,Repeat,我有一个字符串“2017-03-05-02-10-1078205”,我想将它与这个模式匹配[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]{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}
。显然,我用这个模式解决了我的问题
尽管如此,我还是想知道语法{n}
是否有问题。它是否由CMake支持?如果没有,如何使用CMake定义特定的重复次数
我使用的是旧的CMake版本(2.8.11.2)。根据,它不支持{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\u进程
来解决这个问题。
例如,在linux上使用echo
和grep
:
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的跨平台特性。您可以使用
[0-9][0-9][0-9][0-9][0-9]([0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])+
作为解决方法。是的,我知道,但我们失去了正则表达式的紧凑性:(