BIGAL Posted yesterday at 12:05 AM Posted yesterday at 12:05 AM (edited) I am going around in circle and not sure why. I have a Mtext a simple 3 line Mtext (setq txt "Abcdef\\Pghijk\\Plmnop") desired text for Excel a multi line cell "Abcdef\nghijk\nlmnop" thought something like this should work. Tried all sorts of combinations. Note a dbl \\ will leave the P behind but get multi lines. (setq txt (vl-string-translate "\\P" "\n" txt)) I have all the other code done putting all the object values int Excel cells. Think Dataextract. Edited yesterday at 12:07 AM by BIGAL Quote
rlx Posted yesterday at 01:19 AM Posted yesterday at 01:19 AM (defun c:t1 ( / txt1 delimiter txt2 ) (setq txt1 "Abcdef\\Pghijk\\Plmnop") (setq delimiter "\\P") (setq txt2 (pjk-Strparse txt1 delimiter)) (princ (strcat "\ntxt2 = " (vl-prin1-to-string txt2))) (princ) ) ;;; https://www.cadtutor.net/forum/topic/95913-replace-p-in-a-variable/ ;|============================================================================== Function Name: (pjk-StrParse) Arguments: str = String; String to process del = String; Delimiter to separate the string Usage: (pjk-StrParse <string> <delimter>) Returns: List; A list of strings Description: Separates a string into a list of strings using a specified delimiter string; uses recursion. ================================================================================|; (defun pjk-StrParse (str del / pos) (if (and str del) (if (setq pos (vl-string-search del str)) (cons (substr str 1 pos) (pjk-StrParse (substr str (+ pos 1 (strlen del))) del)) (list str) ) ) ) ;; End Function (pjk-StrParse) Quote
Danielm103 Posted yesterday at 03:05 AM Posted yesterday at 03:05 AM You can build a lisp function in Python that will explode the MText into fragments @Ap.Command() def doit(): try: ps, id, pnt = Ed.Editor.entSel("\nSelect: ", Db.MText.desc()) mt = Db.MText(id) frags = mt.getFragments() val = "" for frag in frags: val += ' ' val += frag[Db.MTextFragmentType.kTextValue] print(frags) print(val) except Exception: print(traceback.format_exc()) [PyGe.Point3d(18.25565516926225,16.52816695203355,0.00000000000000), PyGe.Vector3d(0.00000000000000,0.00000000000000,1.00000000000000), PyGe.Vector3d(1.00000000000000,0.00000000000000,0.00000000000000), 'Hello', None, None, PyGe.Point2d(0.63669849931787,0.20327421555252), 0.2, 1.0, 0.0, 1.0, <PyDb.EntityColor object at 0x000001AD0C1D0F90>, 0, 0, 0, 0, 0, [PyGe.Point3d(0.00000000000000,0.00000000000000,0.00000000000000), PyGe.Point3d(0.00000000000000,0.00000000000000,0.00000000000000)], [PyGe.Point3d(0.00000000000000,0.00000000000000,0.00000000000000), PyGe.Point3d(0.00000000000000,0.00000000000000,0.00000000000000)], [PyGe.Point3d(0.00000000000000,0.00000000000000,0.00000000000000), PyGe.Point3d(0.00000000000000,0.00000000000000,0.00000000000000)], 'Arial', False, False] [PyGe.Point3d(19.29427328818596,15.86150028536689,0.00000000000000), PyGe.Vector3d(0.00000000000000,0.00000000000000,1.00000000000000), PyGe.Vector3d(1.00000000000000,0.00000000000000,0.00000000000000), 'World', None, None, PyGe.Point2d(0.72960436562074,0.20327421555252), 0.2, 1.0, 0.0, 1.0, <PyDb.EntityColor object at 0x000001AD0C1D1770>, 0, 0, 0, 0, 0, [PyGe.Point3d(0.00000000000000,0.00000000000000,0.00000000000000), PyGe.Point3d(0.00000000000000,0.00000000000000,0.00000000000000)], [PyGe.Point3d(0.00000000000000,0.00000000000000,0.00000000000000), PyGe.Point3d(0.00000000000000,0.00000000000000,0.00000000000000)], [PyGe.Point3d(0.00000000000000,0.00000000000000,0.00000000000000), PyGe.Point3d(0.00000000000000,0.00000000000000,0.00000000000000)], 'Arial', False, False] Hello World 1 Quote
Lee Mac Posted 13 hours ago Posted 13 hours ago 14 hours ago, BIGAL said: (setq txt (vl-string-translate "\\P" "\n" txt)) Per the documentation, (vl-string-translate) replaces by character, not by string. Hence, for the arguments you have specified, the backslash ("\\") will be replaced by the newline character ("\n") and the "P" does not have a replacement character. Instead, since you're looking to substitute one string for another, you should use the (vl-string-subst) function (in a loop so as to replace all occurrences). I have written a String Subst wrapper which you could call in the following way: (LM:stringsubst "\n" "\\P" "Abcdef\\Pghijk\\Plmnop") 1 Quote
BIGAL Posted 5 hours ago Author Posted 5 hours ago (edited) Thanks everyone, the answer sometimes is just looking at you and waving back. Need to double check help in future. (while (wcmatch txt "*\P*") (setq txt (vl-string-subst "\n" "\\P" txt 1)) ) Screen Recording 2025-11-24 093452.mp4 Edited 5 hours ago by BIGAL Quote
Lee Mac Posted 4 hours ago Posted 4 hours ago You're missing the double backslash ("*\\P*") Also, note that the use of vl-string-subst with a constant starting position and wcmatch to test the content is dangerous in general as this will result in an infinite loop if the replacement string contains the find string, e.g. consider replacing "new" with "knew" - your code would loop indefinitely. Quote
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.