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