Jump to content

Parse numbers from a text string


RepCad

Recommended Posts

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 by amir0914
Link to comment
Share on other sites

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?

  • Like 1
Link to comment
Share on other sites

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 by Roy_043
  • Like 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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")

 

  • Like 1
Link to comment
Share on other sites

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 by Roy_043
  • Like 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

  • Like 1
Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...