【问题标题】:How to use Cmake string regex replace to get last part of path url from multiple instances of path urls and replace the whole path with it如何使用Cmake字符串正则表达式替换从路径url的多个实例中获取路径url的最后一部分并用它替换整个路径
【发布时间】:2021-12-24 01:48:39
【问题描述】:

所以我的字符串如下:

TESTA:C:/Users/mycode/dir_a/./result_a/out_stra.bin;C:/Users/mycode/dir_b/./result_b/out_strb.bin;C:/Users/mycode/dir_c/./result_c/out_strc.bin
TESTB:C:/Users/mycode/dir_a/./result_a/out_stra.bin;C:/Users/mycode/dir_b/./result_b/out_strb.bin;C:/Users/mycode/dir_c/./result_c/out_strc.bin
TESTC:C:/Users/mycode/dir_a/./result_a/out_stra.bin;C:/Users/mycode/dir_b/./result_b/out_strb.bin;C:/Users/mycode/dir_c/./result_c/out_strc.bin

我需要的输出是:

TESTA:out_stra.bin;out_strb.bin;out_strc.bin
TESTB:out_stra.bin;out_strb.bin;out_strc.bin
TESTC:out_stra.bin;out_strb.bin;out_strc.bin

我尝试使用字符串正则表达式,但没有得到所需的输出。这是我尝试过的:

string(REGEX REPLACE "C:/Users/mycode/.*/\./.*/"
       "" TEMP
       <inputfilecontent>) 

将不胜感激。谢谢。

【问题讨论】:

  • 尝试[A-Z]:(/([^/;]+))+ 并替换为\\2。不确定您的情况是否正确,可能是string(REGEX REPLACE "[A-Z]:(/([^/;]+))+" "\\2" TEMP ${inputfilecontent})
  • 嗨@WiktorStribiżew 感谢您的建议,它只给了我最后一个 bin 并忽略了之前的其他 bin 文件
  • 奇怪,好像值之间没有;。也许路径中没有空格?试试[A-Z]:(/([^/; \t\n]+))+
  • @deb:在 Stack Overflow 上,作者完全允许修改问题以添加有关问题的信息。因此,与其删除您的old question 并询问新的相同问题,不如编辑旧的。
  • 非常感谢@WiktorStribiżew 最后一个建议对我解析具有相似路径字符串的多行有很大帮助(我已经基于此修改了问题),但我仍然为每一行得到一个bin,所以用“;”分隔的“.bin”没有以某种方式连接

标签: regex cmake cmake-language


【解决方案1】:

Regex . 匹配任何字符,包括分号 (;) 和换行符 (\n)。这就是为什么您的正则表达式的.* 部分匹配您文件的所有中间“记录”。

因此,您需要将正则表达式中的 . 替换为 [^;\n],这与您的记录的分隔符不匹配:

string(REGEX REPLACE "C:/Users/mycode/[^;\n]*/\./[^;\n]*/"
       "" TEMP
       "<inputfilecontent>")

另请注意,您需要将文件内容括在双引号中。问题是您的文件包含分号 ;,CMake 将不带引号的形式解释为 arguments delimiters

string(REGEX REPLACE) 的特殊之处在于它连接了所有输入参数,因此没有引号的分号将丢失。


可运行示例:

cmake_minimum_required(VERSION 3.10)

project(replace_lines)

set(LINES
[=[
TESTA:C:/Users/mycode/dir_a/./result_a/out_stra.bin;C:/Users/mycode/dir_b/./result_b/out_strb.bin;C:/Users/mycode/dir_c/./result_c/out_strc.bin
TESTB:C:/Users/mycode/dir_a/./result_a/out_stra.bin;C:/Users/mycode/dir_b/./result_b/out_strb.bin;C:/Users/mycode/dir_c/./result_c/out_strc.bin
TESTC:C:/Users/mycode/dir_a/./result_a/out_stra.bin;C:/Users/mycode/dir_b/./result_b/out_strb.bin;C:/Users/mycode/dir_c/./result_c/out_strc.bin
]=]
)
message(STATUS "Lines: ${LINES}")

string(REGEX REPLACE "C:/Users/mycode/[^;\n]*/\./[^;\n]*/"
    "" RESULT
    "${LINES}"
)

message(STATUS "Result: ${RESULT}")

输出:

-- Lines: TESTA:C:/Users/mycode/dir_a/./result_a/out_stra.bin;C:/Users/mycode/dir_b/./result_b/out_strb.bin;C:/Users/mycode/dir_c/./result_c/out_strc.bin
TESTB:C:/Users/mycode/dir_a/./result_a/out_stra.bin;C:/Users/mycode/dir_b/./result_b/out_strb.bin;C:/Users/mycode/dir_c/./result_c/out_strc.bin
TESTC:C:/Users/mycode/dir_a/./result_a/out_stra.bin;C:/Users/mycode/dir_b/./result_b/out_strb.bin;C:/Users/mycode/dir_c/./result_c/out_strc.bin

-- Result: TESTA:out_stra.bin;out_strb.bin;out_strc.bin
TESTB:out_stra.bin;out_strb.bin;out_strc.bin
TESTC:out_stra.bin;out_strb.bin;out_strc.bin

【讨论】:

  • 嗨@Tsyvarev 感谢您的建议,不幸的是它根本不会影响字符串,我在输出中得到相同的字符串
  • 我添加了适合我的确切代码。请检查它是否适合您。
  • 嗨@Tsyvarev 这也很有效,非常感谢
【解决方案2】:

感谢你们所有的回答,我能够找到最终的解决方案,在添加@WiktorStribiżew 解决方案之前必须做一些事情。我不得不这样做

string(REGEX REPLACE ";" "\\\\;" output "${input}")

使解决方案与“;”一起工作

【讨论】:

    猜你喜欢
    • 2019-04-15
    • 2021-09-06
    • 2023-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 2021-03-16
    • 2012-11-25
    相关资源
    最近更新 更多