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

后端 未结 2 713
挽巷
挽巷 2020-12-10 12:33

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

STRING(REGEX REPLACE \"/\" \"\\\\\" SourceGroup ${Sourc         


        
相关标签:
2条回答
  • 2020-12-10 13:01

    This is a simpler way to do it

    file(TO_NATIVE_PATH ${MYPATH} MYPATH)
    
    0 讨论(0)
  • 2020-12-10 13:24

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

    0 讨论(0)
提交回复
热议问题