CMake: how to get the backslash literal in Regexp replace?

15,817

Solution 1

The reason is that in a CMake string literal, the backslash is an escape character (just like in C, Java or JavaScript) and in regex, the backslash is an escape character as well.

So to represent a regex as a string literal, you need double escaping. (That's why many "higher level" languages have regex literal notation, BTW.)

The string literal "\\" represents the in-memory string "\" and that's an invalid regex, hence the "ends in a backslash" error.

The string literal "\\\\" represents "\\" in memory which is a valid regex (representing a single backslash).

Solution 2

This is a simpler way to do it:

file(TO_NATIVE_PATH ${MYPATH} MYPATH)
Share:
15,817

Related videos on Youtube

WoutervD
Author by

WoutervD

Updated on May 13, 2022

Comments

  • WoutervD
    WoutervD about 2 years

    I am trying to replace forwardslashes with backslashes. To do that i have the following line of code:

    STRING(REGEX REPLACE "/" "\\" SourceGroup ${SourceGroupPath} )
    

    SourceGroupPath = A/File/Path. SourceGroup is the variable to set the result to.

    The problem i am having is with, the "\\" part to the code. I have tried several ways to getting to use the backslash literal like "\\" and using unicode but nothing seems to work.

    The error i get in CMake is:

    CMake Error at CMakeLists.txt:41 (STRING): string sub-command REGEX, mode REPLACE: replace-expression ends in a backslash.

    Can someone please help me out?

    Thanks,

    Wouter

    • Tomalak
      Tomalak over 13 years
      Have you tried "\\\\"?
  • Abid Rahman K
    Abid Rahman K about 10 years
    what about forward slash?
  • Tomalak
    Tomalak about 10 years
    The forward slash has no meaning in regular expressions. It might have a meaning in the host language, but only if the host language uses forward slashes to delimit regex literals. In this case it must be escaped within the regex literal. (Many host languages work that way, so that you might come to think that the forward slash is somehow special in regular expressions. It's not.)
  • Maxim Suslov
    Maxim Suslov about 9 years
    Result is string(REGEX REPLACE "/" "\\\\" SourceGroup ${SourceGroup})
  • Angew is no longer proud of SO
    Angew is no longer proud of SO about 9 years
    The reasoning about need for double escapes is right, but this has nothing to do with C string literals - that's CMake code in the question, not C.
  • Tomalak
    Tomalak about 9 years
    @Angew I take it that CMake strings work like C strings?
  • Angew is no longer proud of SO
    Angew is no longer proud of SO about 9 years
    @Tomalak In this regard, yes, the principle is the same - backslash is an escape character.
  • Antonio
    Antonio about 9 years
    Here is available the grammar description for cmake
  • Tomalak
    Tomalak about 9 years
    Okay. In any case you are right, it was imprecise of me. I've corrected my post.
  • tresf
    tresf almost 9 years
    Some build environments (such as MSYS) get mixed up and manual path conversion is needed due to 3rd party executables expecting non-posix style paths, hence the need for manual string replacement.