2012-07-22 52 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}") 
相关问题