Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the {n} syntax of regex with 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 doesn't seem to support the syntax {n}. Obviously, I solved my problem with that pattern [0-9-]+_[0-9]+

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 ?

I'm using an old CMake version (2.8.11.2).

like image 905
cromod Avatar asked Mar 07 '16 14:03

cromod


People also ask

What is N in regex?

\n. Matches a newline character.

What does (? I do in regex?

(? i) makes the regex case insensitive. (? c) makes the regex case sensitive.

What is a ZA Z in regex?

For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter.


2 Answers

According to CMake's documentation, it does not support the {n} syntax. Taken from that page:

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).

It does not seem to be a way to define a specific number of repetitions, rather than copying the expression, e.g.:

[0-9]{5}

would become

[0-9][0-9][0-9][0-9][0-9]
like image 159
davir Avatar answered Sep 30 '22 16:09

davir


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()

But we loose the cross-platform aspect of CMake.

like image 28
cromod Avatar answered Sep 30 '22 15:09

cromod