使用Common Lisp替换文件中的正则表达式

gsl*_*gsl 3 lisp file-io idiomatic common-lisp in-place

我正在尝试使用就地修改文件编写Python的正则表达式搜索和替换的Common Lisp版本:

import fileinput, re

for line in fileinput.input(inplace=1, backup='.bak'):
    line = re.sub(r"foo", "bar", line, re.M)
print (line)
Run Code Online (Sandbox Code Playgroud)

这是我能够想到的Common Lisp代码:

(require :cl-ppcre)

(defun in-place-subst (file)
  (with-open-file (stream file :direction :io :if-exists :overwrite)
    (loop for line = (read-line stream nil)
       while line do
         (write-line (cl-ppcre:regex-replace-all "foo" line "bar") stream))))
Run Code Online (Sandbox Code Playgroud)

它有效.现在,替换文本将附加在文件的末尾.我当前的问题是我无法弄清楚如何替换内容.

为了更好地解释,如果file.txt包含:

1 foo
2 bar
3 foobar
Run Code Online (Sandbox Code Playgroud)

打电话后

(in-place-subst "file.txt")
Run Code Online (Sandbox Code Playgroud)

我明白了:

1 foo
2 bar
3 foobar
1 bar
2 bar
3 barbar
Run Code Online (Sandbox Code Playgroud)

而不是正确的替代品:

1 bar
2 bar
3 barbar
Run Code Online (Sandbox Code Playgroud)

我尝试了所有可能的with-open-file选项(来自Successful Lisp),没有成功:

Keyword      Value                Action if File Exists
----------   ------------------   ---------------------------------------
:IF-EXISTS   NIL                  return NIL
:IF-EXISTS   :ERROR               signal an error
:IF-EXISTS   :NEW-VERSION         next version (or error)
:IF-EXISTS   :RENAME              rename existing, create new
:IF-EXISTS   :SUPERSEDE           replace file upon CLOSE
:IF-EXISTS   :RENAME-AND-DELETE   rename and delete existing, create new
:IF-EXISTS   :OVERWRITE           reuse existing file (position at start)
:IF-EXISTS   :APPEND              reuse existing file (position at end)
Run Code Online (Sandbox Code Playgroud)

有人可以向我发送正确的方向,以便功能file.txt以正确的方式呈现吗?

此外,假设当然可以使用Common Lisp 惯用法做什么cl-ppcre

是否有更简洁的方法使用Common Lisp进行就地正则表达式替换?

Ren*_*nzo 8

在Python中,没有原始操作可以"就地"修改文件; 相反,有一个辅助类的功能,fileinput通过首先将文件复制到备份文件,然后读取备份文件并将处理结果写入原始文件,给出了修改文件的错觉.从手册:

可选的就地过滤:如果将关键字参数inplace=1传递给构造函数fileinput.input()或将其传递给FileInput构造函数,则将文件移动到备份文件,并将标准输出定向到输入文件(如果已存在与备份文件同名的文件,它将被默默地替换).这使得编写一个可以重写其输入文件的过滤器成为可能.如果给出了备份参数(通常为backup ='.'),则指定备份文件的扩展名,备份文件保留在周围; 默认情况下,扩展名为".bak",并在输出文件关闭时删除.读取标准输入时禁用就地过滤.

因此,在Common Lisp中执行此操作的方法是模仿 Python代码,首先将文件复制到备份文件,例如使用此函数 my-copy-file,然后编写以下代码:

(defun in-place-subst (file)
  (let ((backup-file (concatenate 'string file ".bak")))
    (my-copy-file file backup-file)
    (with-open-file (in-stream backup-file)
      (with-open-file (out-stream file :direction :output :if-exists :supersede)
        (loop for line = (read-line in-stream nil)
           while line do
             (write-line (cl-ppcre:regex-replace-all "foo" line "bar") out-stream))))))
Run Code Online (Sandbox Code Playgroud)