【求助】批量替换当前文件中的 英文标点符号 至 中文标点符号?!

各位大佬,需求这样一个功能,能够批量替换当前文件中的 英文标点符号至中文标点符号。

例如下面文件:

Emacs是(最好)的“文本”处理系统.

替换为:

Emacs是(最好)的“文本”处理系统。

M-x, query-replace-regexp 替换 ( 上 和 )就可以了。如果需要一次性替换完,就要写个正则,这块我不熟悉。

1 个赞

可能我没有表述清楚,我需要的是一个这样的函数; 能够根据需要,替换需要替换的中英文符号,一次可能就替换一种英文符号,一次可能要替换十几个英文标点符号到中文标点符号!如果每次都用正则,好像不是很方便!

我之前写过一篇文章,实现lz需要的功能。

(setq repl-regexp-list
      '(("(" "(")
        (")" ")")
        ("\\." "。")
        ("," ",")
        ("?" "?")
        ;; ....
        ))

(defun replall--get-repl-regexp-list ()
  (if (bound-and-true-p repl-regexp-list)
      repl-regexp-list
    (message "please set variable 'repl-regexp-list'!")))

(defun replall--regexp (file lst)
  (with-temp-buffer
    (insert-file-contents file)
    (goto-char (point-min))
    (dolist (pair lst)
      (while (re-search-forward (car pair) nil t)
        (replace-match (cadr pair)))
      (goto-char (point-min)))
    (write-file file)))

(defun replall-regexp-in-curr-buffer ()
  (interactive)
  (let ((curr-file (buffer-file-name (current-buffer)))
        (repl-list (replall--get-repl-regexp-list)))
    (replall--regexp curr-file repl-list)))

repl-regexp-list变量放需要替换的符号或文本对:(被替换 替换者),设置完毕后调用 replall-regexp-in-curr-buffer 命令。

2 个赞

我之前参考过李杀的这篇

另外引号等配对的符号需要注意,英文是不分前后的,需要写点复杂的条件,我之前是这样处理的:".*‘([^’]*).*"

话说你题目里的标点符号似乎就不够规范 :dog:

1 个赞

谢谢您提供的帮助,您的答案正式我所需求的!

谢谢您提供的建议!