2012-08-15 35 views
2

我試圖格式選項卡的多行分隔下面調用replace-match時,如何評估替換字符串中的lisp表達式?

ID Name Duration Start_Date Finish_Date Predecessors Successors Resource_Group Deadline Constraint_Type 

形式的數據轉換成使用下面的Lisp代碼字段列表。

(while (re-search-forward "\\(.*\\)\t\\(.*\\)\t\\(.*\\)\t\\(.*\\)\t\\(.*\\)\t\\(.*\\)\t\\(.*\\)\t\\(.*\\)\t\\(.*\\)\t\\(.*\\)\t\\(.*\\)\t\\(.*\\)\t\\(.*\\)\t\\(.*\\)\t\\(.*\\)\t\\(.*\\)" nil t) 
    (replace-match 
"* \\2 
    :PROPERTIES: 
    :task_id: \\1 
    :duration: \\3 
    :start: \\4 
    :finish: \\5 
    :predecessors: \\6 
    :successors: \\7 
    :resource_group: \\8 
    :deadline: \\9 
    :constraint_type: \\,(match-string 10) 
    :END:" 
nil nil)) 

代碼按預期執行,直到達到第10個回參考匹配的字符串。我發現一個解決方案來反引用大於9的組是使用lisp函數(match-string 10)。當以交互方式使用replace-regexp時,如果替換字符串中的lisp代碼被前綴爲'\,',並且./(match-string 10)的行爲與我期望的交互地調用replace-regexp時相同;

但是,上述代碼塊中的\\,(匹配字符串10)會產生錯誤。我已經嘗試了一,二,三,四等'',但它要麼產生相同的錯誤,要麼打印字符串。有誰知道使用此功能的方法或參考大於9的組號的方法嗎?

非常感謝!

+0

你應該也可以upvote你自己選擇的正確答案。 – 2012-08-16 00:48:14

+0

我該如何upvote?我是新人,在常見問題解答中找不到關於投票如何/爲什麼會起作用的任何內容。有關如何工作的任何提示將有所幫助,謝謝。 – user1593649 2012-08-17 13:44:22

回答

5

而不是在替換字符串中使用\DIGIT序列,該字符串似乎不支持大於9的數字,您可以自己構建替換字符串。喜歡的東西:

(replace-match 
(concat "* " (match-string 2) "\n" 
     " :PROPERTIES:\n" 
     " :task_id: "   (match-string 1) "\n" 
     " :duration: "  (match-string 3) "\n" 
     " :start: "   (match-string 4) "\n" 
     " :finish: "   (match-string 5) "\n" 
     " :predecessors: " (match-string 6) "\n" 
     " :successors: "  (match-string 7) "\n" 
     " :resource_group: " (match-string 8) "\n" 
     " :deadline: "  (match-string 9) "\n" 
     " :constraint_type: " (match-string 10) "\n" 
     " :END:") 
nil t) 

哦,順便說一句,\,(...)建設不replace-match支持,只能通過query-replace-regexp

編輯:另請注意,\,(...)不會簡單地進入替換字符串。那裏有相當多的魔法。在窗簾後面使用C-x ESC ESCquery-replace-regexp

+0

謝謝!這很好,謝謝你指出\,(...)不被replace-match支持。 – user1593649 2012-08-16 00:49:40

相關問題