Re: Looking for better replace-regexp in CLISP
Jean Louis <[email protected]>
| Newsgroups | gmane.lisp.clisp.general |
|---|---|
| Message-ID | <[email protected]> |
Oh yes, it is not perfect. But CLISP users maybe have such function to
replace? by using regexp: to search?
I am using it for simpler string replacement, like
"Hello NAME, happy new year"
On Thu, Dec 29, 2016 at 06:34:58PM +0100, Pascal Bourguignon wrote:
> > (defun replace-string (var1 var2 string)
> > (let ((split (regexp:regexp-split var1 string))) ; regexp-split is in CLISP implementation
> > (format nil "~{~a~}" (butlast (flatten (map 'list #'(lambda (x) (cons x var2)) split))))))
> >
> > (defun flatten (obj)
> > "From Rosetta code: https://rosettacode.org/wiki/Flatten_a_list#Common_Lisp"
> > (do* ((result (list obj))
> > (node result))
> > ((null node) (delete nil result))
> > (cond ((consp (car node))
> > (when (cdar node) (push (cdar node) (cdr node)))
> > (setf (car node) (caar node)))
> > (t (setf node (cdr node))))))
> >
>
>
> We can simplify it:
>
> CL-USER> (defun replace-string (var1 var2 string)
> (let ((split (regexp:regexp-split var1 string)))
> (format nil "~{~a~}" (rest (mapcan (lambda (x) (list var2 x)) split)))))
>
> REPLACE-STRING
>
> But there’s still a fundamental bug:
>
> CL-USER> (replace-string "été" "fut" "0été1234été5678")
> "0fut34fut78"
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
clisp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/clisp-list