Regexing for 2 periods? `..`

The documentation is a bit misleading. You basically need to escape “twice”. Once for the CMake parser itself, and once for the regex engine.

For instance, if you write

if ("D:\\foobar" MATCHES "\\")
    message(STATUS "${CMAKE_MATCH_0}")
endif()

then the regex engine actually gets \, which is not a valid regex, and you get an error like:

RegularExpression::compile(): Trailing backslash.
RegularExpression::compile(): Error in compile.
CMake Error at D:/dev/CMExt/regex.cmake:3 (if):
  if given arguments:

    "D:\\foobar" "MATCHES" "\\"

  Regular expression "\" cannot compile

In your case, you need to write:

if ("D:/foobar/../" MATCHES "\\.\\.")
    message(STATUS "${CMAKE_MATCH_0}")
endif()

I hope this helps!

3 Likes