2012-07-22 41 views
0

我有一個文本文件中的兩個變量,我想要詳述。 [V1]和[V2]。當我運行我的程序時,它只會改變其中的一個。爲什麼?Cmake和正則表達式不按預期工作

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

後,我已經運行此我的文件看起來liket這樣的:(它永遠不會刪除V2)

的應用是目前河

主要功能 運行Hello r時,用戶在stdout上顯示文本「Hello World」。

History 
[v2] 
r 
a 
b 
c 
d 
+0

請問該文件看看啓動?請使用代碼標籤,以便我們也可以看到它有多少行。 – lynxlynxlynx 2012-07-22 11:18:12

+0

該應用程序目前是[v1]。 主要功能運行Hello [v1]時,用戶在stdout上顯示文本「Hello World」。 歷史 [v2] a b c d – user1540911 2012-07-22 12:02:11

+0

對不起,但我無法對您的寫作有足夠的理解。 – lynxlynxlynx 2012-07-22 12:29:05

回答

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}") 
相關問題