Zykl0 Posted September 20, 2017 Share Posted September 20, 2017 Hello, I try to find a lisp that will search and replace text within a user selection. This piece of code that I found on a very old forums on the net do almost exactly what I need. The only issue is I want to replace single word inside sentence and this code replace the word only if it's alone. (defun c:TRANSLATE() (command "cmdecho" "0") (command "_.undo" "begin") (setq mylist (list (list "APPLE" "PEAR") (list "BANANA" "STRAWBERRY") ) ) (setq sset (ssget '((0 . "TEXT")))) (setq i 0) (while (< i (sslength sset)) (setq obj (ssname sset i)) (setq str (cdr (assoc 1 (setq lst (entget obj))))) (princ str) (princ "\n") (foreach item mylist (if (eq str (nth 0 item)) (progn (setq newstr (nth 1 item)) (if newstr (entmod (subst (cons 1 newstr)(assoc 1 lst) lst)) ) ) ) ) (setq i (1+ i)) ) (command "cmdecho" "1") (command "_.undo" "end") ) Can someone modify it to make it work for me? Thank you Quote Link to comment Share on other sites More sharing options...
ronjonp Posted September 20, 2017 Share Posted September 20, 2017 Quick mod: (defun c:translate (/ el mylist sset str) (command "cmdecho" "0") (command "_.undo" "begin") (setq mylist '(("APPLE" "PEAR") ("STRAWBERRY" "BANANA"))) (if (setq sset (ssget ":L" '((0 . "TEXT")))) (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex sset))) (setq str (cdr (assoc 1 (setq el (entget e))))) (foreach item mylist (if (vl-string-search (car item) str) (progn (while (vl-string-search (car item) str) (setq str (vl-string-subst (cadr item) (car item) str)) ) (entmod (subst (cons 1 str) (assoc 1 el) el)) ) ) ) ) ) (command "cmdecho" "1") (command "_.undo" "end") (princ) ) Quote Link to comment Share on other sites More sharing options...
Grrr Posted September 20, 2017 Share Posted September 20, 2017 This subfunction might be handy, but I'm unable to test it ATM: ; (_ReplaceWords ; "APPLE IS MY FAVOURITE FRUIT BUT I HAVE ONLY STRAWBERRYS" ; '(("APPLE" "PEAR") ("STRAWBERRY" "BANANA")) ; ) (defun _ReplaceWords ( str aL ) (mapcar '(lambda (old new) (while (vl-string-search old str) (setq str (vl-string-subst new old str)))) (mapcar 'car aL) (mapcar 'cadr aL) ) str ); defun _ReplaceWords Quote Link to comment Share on other sites More sharing options...
Zykl0 Posted September 21, 2017 Author Share Posted September 21, 2017 Thank you very much guys this is working flawlessly! Quote Link to comment Share on other sites More sharing options...
Roy_043 Posted September 21, 2017 Share Posted September 21, 2017 Note: (while (vl-string-search old str) ...) is problematic if the new string contains the old string: '(("APPLE" "APPLES")). Here is my suggestion: ; (String_Subst "aabbaacc" "aa" "xx") => "xxbbxxcc" ; (String_Subst "aabbaacc" "aa" "xxaa") => "xxaabbxxaacc" (defun String_Subst (str old new / idx) (setq idx 0) (while (setq idx (vl-string-search old str idx)) (setq str (vl-string-subst new old str idx)) (setq idx (+ idx (strlen new))) ) str ) Quote Link to comment Share on other sites More sharing options...
ronjonp Posted September 21, 2017 Share Posted September 21, 2017 Note: (while (vl-string-search old str) ...) is problematic if the new string contains the old string: '(("APPLE" "APPLES")). Here is my suggestion: ; (String_Subst "aabbaacc" "aa" "xx") => "xxbbxxcc" ; (String_Subst "aabbaacc" "aa" "xxaa") => "xxaabbxxaacc" (defun String_Subst (str old new / idx) (setq idx 0) (while (setq idx (vl-string-search old str idx)) (setq str (vl-string-subst new old str idx)) (setq idx (+ idx (strlen new))) ) str ) Good call Roy Quote Link to comment Share on other sites More sharing options...
Grrr Posted September 21, 2017 Share Posted September 21, 2017 Note: (while (vl-string-search old str) ...) is problematic if the new string contains the old string: '(("APPLE" "APPLES")). Here is my suggestion: ... Thanks Roy, I didn't think of that problem! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.