我在Emacs中创建了一个带有重新构建器的正则表达式。我用C-c C-w将它复制到杀戮戒指。
杀人戒指显示:
"\\(defun\\)"
首先,我修改了复制功能以摆脱“”。
\\(defun\\)
我的另一个问题是,kill-ring中的正则表达式包含双反斜杠,使得它无法用于像 query-replace-regexp 这样的函数,我想把它从kill中拉回来 - 环。
这些函数需要单个反斜杠,比如
\(defun\)
所以我认为我可以用'\'替换'\\',然后将其复制到kill-ring中:
(replace-regexp-in-string "\\\\" "\\" "\\(defun\\)" nil t)
执行该功能时,迷你缓冲区显示“\\(defun \\)”而不是“\(defun \)”。
我做错了什么?
答案 0 :(得分: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)))
答案 1 :(得分: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 :(得分:1)
re-builder + .el有一些重新构建的扩展,它提供了使用query-replace-regexp
和replace-regexp.
重新构建器的功能
答案 3 :(得分:0)
这样的东西?
(defun my-yank-unstring ()
(interactive)
(insert (car (read-from-string (current-kill 0)))))
(感谢Trey的(current-kill 0)
位。)
答案 4 :(得分: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)))