0

我想交换一个文本文件中有两个变量。[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 时,用户会在标准输出上看到文本“Hello World”。

History
[v2]
r
a
b
c 
d
4

1 回答 1

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}")
于 2012-07-23T15:35:11.830 回答