Search code examples
regexcmake

check if a string ends with a name in CMake


I want to check if the CMAKE_SOURCE_DIR variable ends with a specific name. I need to use MATCHES for it, but it does not seems to work.

I've written:

if(CMAKE_SOURCE_DIR MATCHES "*MyFolderName")
# code
endif()

But it does not work. I obtain the following error:

RegularExpression::compile(): ?+* follows nothing.
RegularExpression::compile(): Error in compile

What can I do in order to fix the match?


Solution

  • Usually, in regular expressions "*" means "repeat preceding zero or more times". CMake is no exception. To match the end of of a string, use $:

    CMAKE_SOURCE_DIR MATCHES "MyFolderName$"
    

    CMake regular expressions are described here.