• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

CMake:在 Regexp 替换获取反斜杠文字?

用户头像
it1352
帮助1

问题说明

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

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.

有人可以帮我吗?

谢谢,

沃特

正确答案

#1

原因是在 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
系列文章
更多 icon
同类精品
更多 icon
继续加载