Here is the challenger, convert any string into a number, this could be simple strings like:
"100"
"100.45"
or more complex strings like:
"THIS IS A TEST 100 TEST"
For the last example, the value should return 100.
Additionally, all MText and other formatting should be ignored.
Here is my entry:
(defun Str2Num (Str / List_Check List_Numbers List_Characters Results Character)
;Supporting Functions
(defun LM:UnFormat ( str mtx / _replace rx )
;;-------------------=={ UnFormat String }==------------------;;
;; ;;
;; Returns a string with all MText formatting codes removed. ;;
;;------------------------------------------------------------;;
;; Author: Lee Mac, Copyright © 2011 - www.lee-mac.com ;;
;;------------------------------------------------------------;;
;; Arguments: ;;
;; str - String to Process ;;
;; mtx - MText Flag (T if string is for use in MText) ;;
;;------------------------------------------------------------;;
;; Returns: String with formatting codes removed ;;
;;------------------------------------------------------------;;
(defun _replace ( new old str )
(vlax-put-property rx 'pattern old)
(vlax-invoke rx 'replace str new)
)
(if (setq rx (vlax-get-or-create-object "VBScript.RegExp"))
(progn
(setq str
(vl-catch-all-apply
(function
(lambda ( )
(vlax-put-property rx 'global actrue)
(vlax-put-property rx 'multiline actrue)
(vlax-put-property rx 'ignorecase acfalse)
(foreach pair
'(
("\032" . "\\\\\\\\")
(" " . "\\\\P|\\n|\\t")
("$1" . "\\\\(\\\\[ACcFfHLlOopQTW])|\\\\[ACcFfHLlOopQTW][^\\\\;]*;|\\\\[ACcFfHLlOopQTW]")
("$1$2/$3" . "([^\\\\])\\\\S([^;]*)[/#\\^]([^;]*);")
("$1$2" . "\\\\(\\\\S)|[\\\\](})|}")
("$1" . "[\\\\]({)|{")
)
(setq str (_replace (car pair) (cdr pair) str))
)
(if mtx
(_replace "\\\\" "\032" (_replace "\\$1$2$3" "(\\\\[ACcFfHLlOoPpQSTW])|({)|(})" str))
(_replace "\\" "\032" str)
)
)
)
)
)
(vlax-release-object rx)
(if (null (vl-catch-all-error-p str))
str
)
)
)
)
;End Supporting Functions
(setq Str (LM:Unformat Str T))
(setq List_Check (list 45 46 48 49 50 51 52 53 54 55 56 57)
List_Characters (vl-string->list Str)
)
(foreach Character List_Characters
(if (member Character List_Check)
(setq List_Numbers (cons Character List_Numbers))
(setq List_Numbers (cons 32 List_Numbers))
)
)
(setq Results (vl-string-trim " " (vl-list->string (reverse List_Numbers))))
(if (= (substr Results 1 1) ".")
(setq Results (strcat "0" Results))
)
(read Results)
)