0

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.

deb
  • 15
  • 7
  • Try `[A-Z]:(/([^/;]+))+` and replace with `\\2`. Not sure about the right syntax in your case, probably, `string(REGEX REPLACE "[A-Z]:(/([^/;]+))+" "\\2" TEMP ${inputfilecontent})` – Wiktor Stribiżew Nov 11 '21 at 15:16
  • Hi @WiktorStribiżew thanks for your suggestion, its only giving me the last bin and ignoring the other bin files before that – deb Nov 11 '21 at 15:46
  • Strange, as if you did not have `;` between the values. Maybe there are no spaces in the paths? Try `[A-Z]:(/([^/; \t\n]+))+` – Wiktor Stribiżew Nov 11 '21 at 15:48
  • @deb: On Stack Overflow a question is perfectly allowable to be modified by the author for add information about the problem. So, instead of deleting your [old question](https://stackoverflow.com/questions/69928627/read-each-line-of-a-file-and-replace-a-url-string-in-the-file-with-the-end-part) and asking a new one about the very same problem it is better to edit the old one. – Tsyvarev Nov 11 '21 at 15:56
  • Thanks alot @WiktorStribiżew The last suggestion helped me a lot with parsing multiple lines with similar path strings (I have modified the question based on that), but still I get a single bin for each line, so the ".bin"s seperated with ";" is not concatenated somehow – deb Nov 11 '21 at 16:26
  • That makes me think `;` is not parsed as `;`. Not sure how to help you if you cannot use a delimiter in the regex. – Wiktor Stribiżew Nov 11 '21 at 16:28
  • I have come across [this](https://stackoverflow.com/a/18805127/3832970), try replacing the `;` with `$`. Something like `string(REGEX REPLACE "[A-Z]:(/([^/$ \t\n]+))+" "\\2" TEMP ${inputfilecontent})`. Or, try escaping it, `[A-Z]:(/([^/\; \t\n]+))+`. I see there are so many suggestions, but unfortunately, I cannot test it. – Wiktor Stribiżew Nov 11 '21 at 16:46
  • Or, try escaping it, `[A-Z]:(/([^/\; \t\n]+))+`. I see there are so many suggestions, but unfortunately, I cannot test it. – Wiktor Stribiżew Nov 11 '21 at 16:47
  • Hi @WiktorStribiżew i tested both with escape \; and $ not working. The escape is giving me the last ".bin" like before and is not working at all – deb Nov 11 '21 at 17:03
  • Try https://cmake.org/cmake/help/latest/command/get_filename_component.html with NAME mode. – u-235 Nov 12 '21 at 08:13
  • @WiktorStribiżew +1 – deb Nov 12 '21 at 08:45

2 Answers2

0

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
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • Hi @Tsyvarev thanks for your suggestion, unfortunately its not affecting the strings at all, i am getting the same strings in my output – deb Nov 12 '21 at 07:56
  • I have added the exact code which works for me. Please, check whether it works for you. – Tsyvarev Nov 12 '21 at 08:36
  • Hi @Tsyvarev This worked as well, many thanks – deb Nov 15 '21 at 08:04
0

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

deb
  • 15
  • 7