What is the best way to look for number in a string?

What is the best way to look for number in a string?

dvertz
Collaborator Collaborator
446 Views
2 Replies
Message 1 of 3

What is the best way to look for number in a string?

dvertz
Collaborator
Collaborator

I currently have this code, but it fails in this circumstance.

(setq L:POS 1)
(setq L:ELSTR "(-10.9)")

(while (< L:POS (strlen L:ELSTR))
  (if (numberp (read (substr L:ELSTR L:POS 1)))
    (setq L:POS (strlen L:ELSTR))
    (setq L:CHR (substr L:ELSTR 1 L:POS))
  )
  (setq L:POS (1+ L:POS))
)

 

I have tracked down the error to:
(read (substr L:ELSTR L:POS 1))
; error: malformed list on input

And (substr L:ELSTR L:POS 1) returns:
"("

 

The end result I need is to extract the negative number from inside the parenthesis. But I also need it to extract the number if the parenthesis are not there. Or extract the number if its positive instead of negative.

 

Thanks.

 

Civil 3D 2022,
Windows 10 Pro, x64, Nvidia Quadro P1000
Intel Core i9-11900k; 3.50GHz, 32 GB RAM, 500GB WD BLACK M.2


0 Likes
Accepted solutions (1)
447 Views
2 Replies
Replies (2)
Message 2 of 3

ВeekeeCZ
Consultant
Consultant
Accepted solution

By ASCII number?

 

https://lee-mac.com/parsenumbers.html

 

>>  (car (LM:parsenumbers "(-10.9)"))
>> -10.9

0 Likes
Message 3 of 3

dvertz
Collaborator
Collaborator

Thanks BeekeeCZ

Of course Lee-Mac would have the code. That guy has helped me out on so many occasions.

Civil 3D 2022,
Windows 10 Pro, x64, Nvidia Quadro P1000
Intel Core i9-11900k; 3.50GHz, 32 GB RAM, 500GB WD BLACK M.2


0 Likes