String Replace Between Closing Parenthesis and Forward Slash

String Replace Between Closing Parenthesis and Forward Slash

DGCSCAD
Collaborator Collaborator
754 Views
9 Replies
Message 1 of 10

String Replace Between Closing Parenthesis and Forward Slash

DGCSCAD
Collaborator
Collaborator

How would I add a space (or any character) between ) and \ in a string?

 

Example:

String: "(test)\\p"

Result: "(test) \\p"

 

I'm stumped.

AutoCad 2018 (full)
Win 11 Pro
0 Likes
755 Views
9 Replies
Replies (9)
Message 2 of 10

paullimapa
Mentor
Mentor

Since Autolisp uses the slash as an escape code you would have to double it for it to find a match. So try searching for following:

 

"*)\\\\*"

 

here’s a link to give you some ideas on what functions to try to do a search and replace

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 3 of 10

ec-cad
Collaborator
Collaborator

(setq a "(test)\\p")
(setq b (substr a 1 (- (strlen a) 2)))
(setq add " "); what to add
(setq new (strcat b add "\\p"))

 

ECCAD

0 Likes
Message 4 of 10

DGCSCAD
Collaborator
Collaborator

@paullimapa 

(setq txt "(test)\\p")

(wcmatch "*)\\\\*" txt)

nil

 

wcmatch is a fickle beast.

 

@ec-cad 

That does work under those exact conditions, but I'm needing to find and replace many occurrences, with variances in string length between the parenthesis, which I failed to mention. My apologies.

AutoCad 2018 (full)
Win 11 Pro
0 Likes
Message 5 of 10

paullimapa
Mentor
Mentor

you incorrectly used the wcmatch function...this is the correct format:

(setq txt "(test)\\p")
(wcmatch txt "*)\\*")
Returns: T
(setq txt "(test)\\\\p")
(wcmatch txt "*)\\\\*")
Returns: T

look it up here:

https://help.autodesk.com/view/OARX/2023/ENU/?guid=GUID-EC257AF7-72D4-4B38-99B6-9B09952A53AD

 

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 6 of 10

DGCSCAD
Collaborator
Collaborator

@paullimapa 

In my haste, yes I did. My apologies. I need to step back for a minute.

AutoCad 2018 (full)
Win 11 Pro
0 Likes
Message 7 of 10

paullimapa
Mentor
Mentor

not a problem..I get the formatting of wcmatch confused all the time as well.


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 8 of 10

MrJSmith
Advocate
Advocate

Few things.....

  1.  I am confused as to why you were using wcmatch when you wanted to replace the string.
  2. You used wcmatch incorrectly, it should be string patter, not pattern string. 
    1. (wcmatch txt "*)\\*") => T
  3. Your example suggests this is coming from MTEXT inside AutoCAD? In which case, it would only be "\\" to get the text to replace.
    1. If you are selecting some MTEXT in AutoCAD:

 

(defun replaceAll (oldtext newtext textstring / i n)
	(setq n (strlen newtext))
	(while (setq i (vl-string-search oldtext textstring i))
		(setq 
		textstring (vl-string-subst newtext oldtext textstring i)
		i (+ i n))
	)
	textstring
)

(replaceAll ")\\P" ") \\P" (cdr (assoc 1 (entget (ssname (ssget) 0))))

 

 

  • If you are NOT selecting MTEXT:

 

(setq txt "(test)\\p")
(replaceAll ")\\p" ") \\p" txt)​

 

Message 9 of 10

ronjonp
Advisor
Advisor

@DGCSCAD Try this:

 

(vl-string-subst ") \\p" ")\\p" "(test)\\p")

 

0 Likes
Message 10 of 10

ec-cad
Collaborator
Collaborator

I have a 'partial' solution. See attached .dwg for details.

Does all correctly except those \\\p in Mtext.

Feel free to grab and modify (uses old lisp, no Vlisp).

;; chgtxt.lsp
;; rev 1.0
;; Search 'old' text, replace with 'new' text in TEXT or MTEXT
;;
;; Local Function to check for Old String / Replace with New String..
;;
(defun s_r ( ostr nstr oldtext )
   (if (and (/= ostr nil)(/= nstr nil)(/= oldtext nil)); old string, new string
     (progn
       (setq os ostr ns nstr n_str "" found 0 si 1)
       (setq osl (strlen os))
       (setq nsl (strlen ns))
       (setq s oldtext); pass in old value
        (while (= osl (setq sl (strlen (setq st (substr s si osl)))))
          (if (= st os)
           (progn
            (setq s (strcat (substr s 1 (1- si)) ns (substr s (+ si osl))))
            (setq found 1); Found old string
            (setq si (+ si nsl))
           ); end progn
          (setq si (1+ si))
          ); end if
         ); end while
        (if (= found 1)(setq n_str s)); modified string
    ); end progn
   ); end if
 (princ)
); end function
;;
;; Function to change matching text strings in Text or Mtext
;;
(defun C:chgtxt (/ oldstring newstring )
 (setq oldstring (getstring 1 "\nOld string to replace = ")); e.g. )\\
 (setq newstring (getstring 1 "\nNew string replacement = ")); e.g. ADDED Something
 (setq ss nil C 0)
 (setq ss (ssget "X" (list (cons 0 "*TEXT"))))
 (if ss
  (progn
   (repeat (sslength ss)
    (setq ent (entget (ssname ss C))); entity list
    (setq otxt (cdr (assoc 1 ent))); just the old text string
    (s_r oldstring newstring otxt)
    (if (= found 1)
      (entmod (setq ent (subst (cons 1 n_str)(cons 1 otxt) ent)))
    ); if
    (setq C (+ C 1))
   ); repeat
  ); progn
 ); if
); function

 

ECCAD 

0 Likes