我想在文本文件中输入两个变量。 [V1]和[V2]。当我运行我的程序时,它只会更改其中一个。为什么呢?
#Read up into variable
file (STRINGS "myfile.txt" v1)
#Store text to replace in variable (we need to escape the '[' and ']' since we will use a regexp later on)
set(v1_placeholder "\\[v1\\]")
set(v2_placeholder "\\[v2\\]")
#New reading of documents to process
set(doc_files [v1]_Hello_Documentation.txt myfile.txt)
#Lets iterate over each documentation file
foreach(doc_file ${doc_files})
message(STATUS "Proccessing document file: " ${doc_file})
#Read up content of documentation file
file(READ ${doc_file} FILE_CONTENT)
#Remove occurences of [v2] with nothing
string(REGEX REPLACE "${v2_placeholder}" "" MODIFIED_FILE_CONTENT "${FILE_CONTENT}")
#Replace occurences of [v1] with the real variable in the content
string(REGEX REPLACE "${v1_placeholder}" "${V1}" MODIFIED_FILE_CONTENT "${FILE_CONTENT}")
#Replace occurences of [v1] with the real variable in the file name
string(REGEX REPLACE "${v1_placeholder}" "${V1}" MODIFIED_FILE_NAME "${doc_file}")
#Write modified content back into modifed file name
file(WRITE ${MODIFIED_FILE_NAME} "${MODIFIED_FILE_CONTENT}")
#Add the files to the package in dir /doc
install (FILES ${MODIFIED_FILE_NAME} DESTINATION doc)
endforeach(doc_file)
在我运行之后,我的文件会看起来像这样:(它永远不会删除v2。)
该申请目前为r。
主要功能 运行Hello r时,将在stdout上向用户显示文本“Hello World”。
History
[v2]
r
a
b
c
d
答案 0 :(得分:0)
您可能需要将MODIFIED_FILE_CONTENT
而不是FILE_CONTENT
传递给第二次string(REGEX REPLACE
,即:
#Remove occurences of [v2] with nothing
string(REGEX REPLACE "${v2_placeholder}" "" MODIFIED_FILE_CONTENT "${FILE_CONTENT}")
#Replace occurences of [v1] with the real variable in the content
string(REGEX REPLACE "${v1_placeholder}" "${V1}" MODIFIED_FILE_CONTENT "${MODIFIED_FILE_CONTENT}")