我正在尝试格式化以下表单的多行TAB分隔数据
ID Name Duration Start_Date Finish_Date Predecessors Successors Resource_Group Deadline Constraint_Type
Run Code Online (Sandbox Code Playgroud)
使用下面的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))
Run Code Online (Sandbox Code Playgroud)
代码按预期执行,直到达到第10个反向引用的匹配字符串.我发现一个反向引用大于9的组的解决方案是使用lisp函数(match-string 10).当以交互方式使用replace-regexp时,如果替换字符串中的lisp代码前面带有'\,',则会对其进行评估,而./(match-string 10)的行为与我预期的交互式调用replace-regexp时的行为相同;
但是,上面代码块中的\\,(匹配字符串10)会产生错误.我已经尝试了一个,两个,三个,四个等''',但它会产生相同的错误或打印一个文字字符串.有没有人知道使用此功能的方法或引用大于9的组号的方法?
非常感谢!
\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)
Run Code Online (Sandbox Code Playgroud)
哦,顺便说一句,\,(...)施工不受支持replace-match,只有query-replace-regexp.
编辑:另请注意,\,(...)不要简单地进入替换字符串.那里有很多魔力.在幕后使用C-x ESC ESC后偷看query-replace-regexp.