Hi,
Is there a simple search and replace function for a file within lisp?
I want to open a file for writing, and replace every instance of a
certain word. Is there something like regular expressions?
Thanks!
On Feb 12, 6:34 pm, ······@gmail.com wrote:
> Is there a simple search and replace function for a file within lisp?
> I want to open a file for writing, and replace every instance of a
> certain word. Is there something like regular expressions?
Check out CL-PPCRE, a regex library for Common Lisp:
http://weitz.de/cl-ppcre/
Cheers,
Pillsy
On Mon, 12 Feb 2007 15:34:21 -0800, qalex1 wrote:
> Hi,
>
> Is there a simple search and replace function for a file within lisp?
> I want to open a file for writing, and replace every instance of a
> certain word. Is there something like regular expressions?
Here's an example using cl-ppcre:
cl-user> (with-open-file (fs
"/home/lars/programming/lisp/hello-world.lisp")
(let ((seq (make-sequence 'string (file-length fs))))
(read-sequence seq fs)
seq))
"(write-line \"Hello World!\")
"
cl-user> (require :cl-ppcre)
nil
cl-user> (cl-ppcre:regex-replace-all
"Hello"
(with-open-file (fs "/home/lars/programming/lisp/hello-world.lisp")
(let ((seq (make-sequence 'string (file-length fs))))
(read-sequence seq fs)
seq))
"Hi")
"(write-line \"Hi World!\")
"
cl-user>
Using `write-sequence' you can write the result to the file.
--
Lars Rune Nøstdal
http://nostdal.org/
On Tue, 13 Feb 2007 05:25:54 +0100, <···········@gmail.com> wrote:
> Here's an example using cl-ppcre:
Thanks Lars
> cl-user> (cl-ppcre:regex-replace-all
> "Hello"
> (with-open-file (fs "/home/lars/programming/lisp/hello-world.lisp")
> (let ((seq (make-sequence 'string (file-length fs))))
> (read-sequence seq fs)
> seq))
> "Hi")
So, with oversimplifying, thats
(cl-ppcre:regex-replace-all "Old" foo-seq "New")
which is similar to perl (what I'm used to for this task)
$foo-seq =~ s/Old/New/g;
Maybe a little work to get the verbage down...
--
There are no average Common Lisp programmers
Reply-To: email is ignored.
--
Posted via a free Usenet account from http://www.teranews.com
On Feb 12, 6:34 pm, ······@gmail.com wrote:
> Hi,
>
> Is there a simple search and replace function for a file within lisp?
> I want to open a file for writing, and replace every instance of a
> certain word. Is there something like regular expressions?
>
> Thanks!
If you are familiar with Emacs you can try
M-x describe-function RET
replace-string RET
which bring you to find replace.el
Since it is Emacs Lisp, I am not sure whether this is what you are
looking for.
But maybe it gives you a starting point.
Best,
Roland