CMake:如何在 Regexp 替换中获取反斜杠文字? [英] CMake: how to get the backslash literal in Regexp replace?

查看:27
本文介绍了CMake:如何在 Regexp 替换中获取反斜杠文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用反斜杠替换正斜杠.为此,我有以下代码行:

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/文件/路径.SourceGroup 是将结果设置为的变量.

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

我遇到的问题是代码的\"部分.我尝试了几种方法来使用像\"这样的反斜杠文字并使用 unicode 但似乎没有任何效果.

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.

我在 CMake 中得到的错误是:

The error i get in CMake is:

CMakeLists.txt 中的 CMake 错误:41(STRING): 字符串子命令 REGEX,模式替换:替换表达式结束在反斜杠中.

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

有人可以帮我吗?

谢谢,

沃特

推荐答案

原因是在 CMake 字符串文字中,反斜杠是一个转义字符(就像在 C、Java 或 JavaScript 中一样),而在正则表达式中,反斜杠是一个转义字符以及.

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

这篇关于CMake:如何在 Regexp 替换中获取反斜杠文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆