RepCad Posted December 16, 2019 Posted December 16, 2019 (edited) Hi all, I have lots of texts in autocad and I need to a lisp or function to extract numbers from text in a list in the following way: "lili/=0.50@@0.573#0.53" >> (0.50 0.573 0.53) "/= 0.00@ 20" >> (0.00 20) " lili/= 23320: @" >> (23320) ":12" >> (12) Can someone help me? Edited December 16, 2019 by amir0914 Quote
dlanorh Posted December 16, 2019 Posted December 16, 2019 1 hour ago, amir0914 said: Hi all, I have lots of texts in autocad and I need to a lisp or function to extract numbers from text in a list in the following way: "lili/=0.50@@0.573#0.53" >> (0.50 0.573 0.53) "/= 0.00@ 20" >> (0.00 20) " lili/= 23320: @" >> (23320) ":12" >> (12) Can someone help me? Does the (## ## ##) mean you want them as a list? 1 Quote
RepCad Posted December 16, 2019 Author Posted December 16, 2019 5 minutes ago, dlanorh said: Does the (## ## ##) mean you want them as a list? Yes, exactly Quote
Roy_043 Posted December 16, 2019 Posted December 16, 2019 (edited) Something like this perhaps: (defun ParseIt (str) (read (vl-list->string (append '(40) ; "(". (mapcar '(lambda (int) (if (or (= 46 int) (<= 48 int 57)) int 32)) (vl-string->list str) ) '(41) ; ")". ) ) ) ) Edited December 16, 2019 by Roy_043 1 Quote
RepCad Posted December 16, 2019 Author Posted December 16, 2019 23 minutes ago, Roy_043 said: Something like this perhaps: (defun ParseIt (str) (read (vl-list->string (append '(40) ; "(". (mapcar '(lambda (int) (if (or (= 46 int) (<= 48 int 57)) int 32)) (vl-string->list str) ) '(41) ; ")". ) ) ) ) Thanks a lot, but it has a minor problem : lili/=0.50@@0.573#0.53" >> (0.5 0.573 0.53) In this result 0.5 must be 0.50 , it means the program remove the last zero, while I need exact the same numbers in string. Quote
Roy_043 Posted December 16, 2019 Posted December 16, 2019 This then means that you want to extract the numbers as strings, right? Quote
Lee Mac Posted December 16, 2019 Posted December 16, 2019 You may want to consider the 'splitstring' function used by my Incremental Array program: (defun incarray:splitstring ( str / lst ) (setq lst (vl-string->list str)) (read (vl-list->string (vl-list* 40 34 (incarray:split lst (< 47 (car lst) 58))))) ) (defun incarray:split ( lst flg ) (cond ( (null lst) '(34 41)) ( (member (car lst) '(34 92)) (if flg (vl-list* 34 32 34 92 (car lst) (incarray:split (cdr lst) nil)) (vl-list* 92 (car lst) (incarray:split (cdr lst) flg)) ) ) ( (or (< 47 (car lst) 58) (and (= 46 (car lst)) flg (< 47 (cadr lst) 58))) (if flg (vl-list* (car lst) (incarray:split (cdr lst) flg)) (vl-list* 34 32 34 (car lst) (incarray:split (cdr lst) t)) ) ) ( flg (vl-list* 34 32 34 (car lst) (incarray:split (cdr lst) nil))) ( (vl-list* (car lst) (incarray:split (cdr lst) nil))) ) ) Example: _$ (incarray:splitstring "lili/=0.50@@0.573#0.53") ("lili/=" "0.50" "@@" "0.573" "#" "0.53") 1 Quote
Roy_043 Posted December 16, 2019 Posted December 16, 2019 (edited) To parse numbers as strings: ; (KGA_String_TokenizeAlt "lili/=0.50@@0.573#0.53" "0123456789.") (defun KGA_String_TokenizeAlt (str keepSet / ret sub) (setq keepSet (vl-string->list keepSet)) (setq str (vl-string->list str)) (repeat (1+ (length str)) (cond ((vl-position (car str) keepSet) (setq sub (cons (car str) sub)) ) (sub (setq ret (cons (vl-list->string (reverse sub)) ret)) (setq sub nil) ) ) (setq str (cdr str)) ) (reverse ret) ) Edited December 16, 2019 by Roy_043 1 Quote
RepCad Posted December 16, 2019 Author Posted December 16, 2019 3 minutes ago, Roy_043 said: To parse numbers as strings: ; (KGA_String_TokenizeAlt "lili/=0.50@@0.573#0.53" "0123456789.") (defun KGA_String_TokenizeAlt (str keepSet / ret sub) (setq keepSet (vl-string->list keepSet)) (setq str (vl-string->list str)) (repeat (1+ (length str)) (cond ((vl-position (car str) keepSet) (setq sub (cons (car str) sub)) ) (sub (setq ret (cons (vl-list->string (reverse sub)) ret)) (setq sub nil) ) ) (setq str (cdr str)) ) (reverse ret) ) Thank you Roy_043 , that's great. Quote
Lee Mac Posted December 16, 2019 Posted December 16, 2019 I guess it depends on how much validation you require on whether or not a set of characters really represent a number. 1 Quote
hanhphuc Posted December 16, 2019 Posted December 16, 2019 8 hours ago, Lee Mac said: I guess it depends on how much validation you require on whether or not a set of characters really represent a number. agree! 1e2 1e-8 1.2e3 1.23e-4 1e+3 1 Quote
RepCad Posted December 20, 2019 Author Posted December 20, 2019 Hello again and thanks to Roy_043 and Lee Mac for replying.I have another question. How can I check a string that it is numeric? ("14") >> T ("15.30") >> T ("GtH") >> F ("FG120") >> F (")&L") >> F Quote
rlx Posted December 20, 2019 Posted December 20, 2019 (distof "14") (distof "15.30") (distof "GtH") (distof "FG120") (distof ")&L") 1 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.