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).
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcabbfa
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20