Home > OS >  How to use Cmake string regex replace to get last part of path url from multiple instances of path u
How to use Cmake string regex replace to get last part of path url from multiple instances of path u

Time:11-12

So my string is as follows:

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

My required output is:

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

I tried with string regex but I'm not getting the required output. Here is what I tried:

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

will be grateful for any help. Thanks.

CodePudding user response:

Regex . matches any character, including semicolon (;) and newline (\n). This is why part .* of your regex matches all intermediate "records" of your file.

So you need to replace . in your regex with [^;\n], which doesn't match delimiters of your records:

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

Note also, that you need to enclose content of your file into double quotes. The thing is that your file contains semicolons ;, which in unquoted form are interpreted by CMake as arguments delimiters.

Specific of string(REGEX REPLACE) is that it concatenates all input arguments, so without quotes semicolons will be lost.


Runnable example:

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}")

Output:

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

CodePudding user response:

Thanks for all you answers guys, I was able to find the final solution, had to do something before adding @WiktorStribiżew solution. I had to do

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

to make the solution work with ";"

  • Related