DuanJinHui Posted March 18, 2017 Posted March 18, 2017 Remove [####.##.##] from a string eg. "[2017.03.06][2016.12.10]Blueprint.dwg" When removed "Blueprint.dwg" Quote
hanhphuc Posted March 18, 2017 Posted March 18, 2017 (edited) Remove [####.##.##] from a string normally we would use wcmatch, alternatively i use list method bcos i see the brackets [##] (setq str "[2017.03.06][2016.12.10]blueprint.dwg") (strcat (vl-string-right-trim " " (apply 'strcat (mapcar ''((x) (strcat (vl-princ-to-string x) " ")) (vl-remove-if 'vl-consp (read (strcat "(" (vl-string-translate "[]" "()" str) ")"))) ) ) ) ".dwg" ) Edited March 18, 2017 by hanhphuc accept string with space , vl-string-right-trim Quote
DuanJinHui Posted March 18, 2017 Author Posted March 18, 2017 normally we would use wcmatch,alternatively i use list method bcos i see the brackets [##] Disadvantage the string must be no spacing, example "blue_print.dwg" "blue print.dwg" failed (setq str "[2017.03.06][2016.12.10]blueprint.dwg") (strcat (vl-princ-to-string (car (vl-remove-if 'vl-consp (read (strcat "(" (vl-string-translate "[]" "()" str ) ")") ) ) ) ) ".dwg") Hi hanhphuc, Thank you so much ! I 'd like to see use wcmatch method. Quote
Grrr Posted March 18, 2017 Posted March 18, 2017 Using wcmatch : ; _$ (foo "[2017.03.06][2016.12.10]Blueprint.dwg") -> "Blueprint.dwg" ; _$ (foo "[2017.03.06][2016.12.10]This is a test.dwg") -> "This is a test.dwg" (defun foo ( s ) (cond ( (wcmatch s "`[####`.##`.##`]`[####`.##`.##`]*`.dwg") (substr s 25) ) ) ) Quote
DuanJinHui Posted March 18, 2017 Author Posted March 18, 2017 Using wcmatch : ; _$ (foo "[2017.03.06][2016.12.10]Blueprint.dwg") -> "Blueprint.dwg" ; _$ (foo "[2017.03.06][2016.12.10]This is a test.dwg") -> "This is a test.dwg" (defun foo ( s ) (cond ( (wcmatch s "`[####`.##`.##`]`[####`.##`.##`]*`.dwg") (substr s 25) ) ) ) Sorry , [####.##.##] maybe is not Two, maybe is one or Three. Quote
Lee Mac Posted March 18, 2017 Posted March 18, 2017 Another, using Regular Expressions: (defun foo ( str / rgx ) (if (setq rgx (vlax-get-or-create-object "vbscript.regexp")) (progn (vlax-put-property rgx 'global actrue) (vlax-put-property rgx 'pattern "\\[\\d{4}\\.\\d{2}\\.\\d{2}\\]") (setq str (vlax-invoke rgx 'replace str "")) (vlax-release-object rgx) str ) ) ) _$ (foo "[2017.03.06]Blueprint.dwg") "Blueprint.dwg" _$ (foo "[2017.03.06][2016.12.10]Blueprint.dwg") "Blueprint.dwg" _$ (foo "[2017.03.06][2016.12.10][2016.12.10]Blueprint.dwg") "Blueprint.dwg" Quote
Grrr Posted March 18, 2017 Posted March 18, 2017 Sorry , [####.##.##] maybe is not Two, maybe is one or Three. Ah, now I understood the task! Here: ; _$ (foo "[2013.12.10]Blueprint.dwg") -> "Blueprint.dwg" ; _$ (foo "[2015.08.10][2016.12.10]This is a test.dwg") -> "This is a test.dwg" ; _$ (foo "[2012.02.13][1991.08.10][2016.12.10]And Another test.dwg") -> "And Another test.dwg" (defun foo ( s ) (cond ( (wcmatch s "`[####`.##`.##`]*`.dwg") (while (wcmatch s "`[####`.##`.##`]*`.dwg") (setq s (substr s 13)) ) ) ) ) Lee your code blews my mind! Quote
Lee Mac Posted March 18, 2017 Posted March 18, 2017 Thanks Grrr Note that your code could become: (defun foo ( s ) (while (wcmatch s "`[####`.##`.##`]*`.dwg") (setq s (substr s 13)) ) s ) This way, the function will return the original string rather than nil if the pattern is not found. Quote
Grrr Posted March 18, 2017 Posted March 18, 2017 Note that your code could become: Thanks Lee, sometimes I overthink the code and can't see easily that it could be shortened. Quote
DuanJinHui Posted March 19, 2017 Author Posted March 19, 2017 Lee , hanhphuc, Grrr, Thank you very much! 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.