2009-08-27 274 views
1

我在Emacs中用重新構建器創建了一個正則表達式。我用C-C C-W把它複製到殺戒上。用Emacs中的單個反斜槓替換雙反斜槓

壓井環顯示:

"\\(defun\\)"

首先,我修改了複製功能,擺脫了「」。

\\(defun\\)

我的另一個問題是,在擊殺環的正則表達式包含雙反斜線,使其不可像功能查詢替換,正則表達式,我想抽出它放回從殺環。

這些功能預計單反斜線,像

\(defun\)

所以我想我可以通過這樣它複製到殺環前更換「\\」與「\」:

(replace-regexp-in-string "\\\\" "\\" "\\(defun\\)" nil t)

執行該函數時,由於minibuffer顯示「\\(defun \\)」而不是「\(defun \)」。

我在做什麼錯?

回答

2

這個工作對我來說:

(defun my-reb-copy() 
    "Copy current RE into the kill ring without quotes and single 
backslashes for later insertion." 
    (interactive) 
    (reb-update-regexp) 
    (let* ((re (with-output-to-string 
       (print (reb-target-binding reb-regexp)))) 
     (str (substring re 2 (- (length re) 2)))) 
    (with-temp-buffer 
     (insert str) 
     (goto-char (point-min)) 
     (while (search-forward "\\\\" nil t) 
     (replace-match "\\" nil t)) 
     (kill-new (buffer-substring (point-min) (point-max)))) 
    (message "Regexp copied to kill-ring"))) 

(define-key reb-mode-map "\C-c\C-t" 'my-reb-copy) 
2

通過'insert'princ將字符串傳遞給您。

(princ "\\(defun\\)") 
-> 
\(defun\) 

我沒有看到明顯的辦法直接做到這一點,這裏似乎與測試字符串我的工作真是令人費解的方式:

(defun my-insert-kill() 
    "convoluted way to replace \\ in strings" 
    (interactive) 
    (princ (with-temp-buffer 
      (insert "(progn (princ ") 
      (insert (current-kill 0)) 
      (insert ") (insert \"\n\") nil)") 
      (eval-print-last-sexp) 
      (backward-sexp) (backward-char) 
      (let ((p (point))) 
      (goto-char (point-min)) 
      (forward-sexp) (forward-char) 
      (buffer-substring (point) p))) 
     (current-buffer))) 
+0

是,在緩衝區評估像*刮*我可以做同樣的事情(而(搜索-forward「\\\\」nil t)(replace-match「\\」nil t)),當然你的解決方案要容易得多,但我需要以某種方式將它封裝在一個命令中。 – cschol 2009-08-27 12:44:11

0

像這樣的事情?

(defun my-yank-unstring() 
    (interactive) 
    (insert (car (read-from-string (current-kill 0))))) 

(感謝特雷爲(current-kill 0)位。)

0

退一步,您可以將變量reb-re-syntax設置爲string而不是默認的read。這會將re-builder的行爲更改爲use single backslashes instead of double

要做到這一點使用自定義:

M-x customize-variable RET rev-re-syntax RET 

或添加到您的。emacs的文件:

(set reb-re-syntax 'string) 

然後直接跳到更換,你可以使用this defun

(defun reb-query-replace (to-string) 
    "Replace current RE from point with `query-replace-regexp'." 
    (interactive 
    (progn (barf-if-buffer-read-only) 
      (list (query-replace-read-to (reb-target-binding reb-regexp) 
             "Query replace" t)))) 
    (with-current-buffer reb-target-buffer 
    (query-replace-regexp (reb-target-binding reb-regexp) to-string))) 
相關問題