From: ······@gmail.com
Subject: search and replace
Date: 
Message-ID: <1171323261.807947.202200@a75g2000cwd.googlegroups.com>
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!

From: Pillsy
Subject: Re: search and replace
Date: 
Message-ID: <1171323827.118471.288860@h3g2000cwc.googlegroups.com>
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
From: Lars Rune Nøstdal
Subject: Re: search and replace
Date: 
Message-ID: <pan.2007.02.13.04.25.53.828298@gmail.com>
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/
From: GP lisper
Subject: Re: search and replace
Date: 
Message-ID: <slrnet4ndr.pfr.spambait@phoenix.clouddancer.com>
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
From: ··········@gmail.com
Subject: Re: search and replace
Date: 
Message-ID: <1171399570.139127.243930@q2g2000cwa.googlegroups.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