请问如何提取字符串中的数值?
(setq string "X-12.0314 Y26.9145 I48.7961 J-14.9177")请问如何提出string中的x,y,I,J值??各数值的位数是不定的,即是可变的。例如x=12.03,y=26,I=148,J=14 (read (strcat "(" (vl-string-translate "XYIJ" " " String) ")")) 这个适用性强点(defun extract-numeric-from-text (text)
(setq str-list (vl-string->list text)) ; 将字符串转换为字符列表
(setq num-list nil) ; 用于存储数字字符
(foreach char str-list
(if (and (>= char 48) (<= char 57)) ; 48 到 57 是数字 0 到 9 的 ASCII 码值
(setq num-list (append num-list (list char)))
(setq num-list (append num-list (list 32)))
)
)
(setq num-str (vl-list->string num-list)) ; 将数字字符列表转换回字符串
) (setq string "X-12.0314 Y26.9145 I48.7961 J-14.9177")
(setq n1 (vl-string-position (ascii "X") string))
(setq n2 (vl-string-position (ascii "Y") string))
(setq n3 (vl-string-position (ascii "I") string))
(setq n4 (vl-string-position (ascii "J") string))
(setq X (substr string (+ n1 2) (- n2 n1 2)))
(setq Y (substr string (+ n2 2) (- n3 n2 2)))
(setq I (substr string (+ n3 2) (- n4 n3 2)))
(setq J (substr string (+ n4 2)))
本帖最后由 zhynt 于 2011-6-21 16:53 编辑
;;将由相同分隔字符分隔的字符串组成表(忽略“空”字串)
(defun zhy_string_tok (sstring sstr / n1 n2 m2 str_1 string_list)
(setq string_list '())
(setq n1 (strlen sstring))
(setq n2 (strlen sstr))
(while (setq m2 (zhy_string_find sstring sstr))
(setq str_1 (substr sstring 1 (- m2 1)))
(setq sstring (substr sstring (+ m2 n2)))
(if (/= str_1 "")
(setq string_list (cons str_1 string_list))
)
)
(if (/= sstring "")
(setq string_list (cons sstring string_list))
)
(reverse string_list)
)
;;======================================================
(defun str (str)
(setq str_lst (zhy_string_tok str " "))
(setq n (length str_lst))
(setq st1 '()
st2 '()
m 0
)
(while (/= n m)
(setq st1 (cons (substr (nthm str_lst) 1 1) st1)
st2 (cons (substr (nthm str_lst) 2) st2)
m (1+ m)
)
)
(setq st1 (reverse st1)
st2 (reverse st2)
)
)
(setq string "X-12.0314 Y26.9145 I48.7961 J-14.9177")
(str string)
st1==>
("X" "Y" "I" "J")
st2==>
("-12.0314" "26.9145" "48.7961" "-14.9177")
zhy_string_find 没有? 没有办法用了
;;;在字符串中搜索指定的字符串,返回发现的第一个字符串出现的位置(第一位为1)
;;;
(defun zhy_string_find (string str / n m na str_1 string str)
(setq n (strlen string))
(setq na (strlen str))
(setq m 0)
(while (/= m n)
(setq m (1+ m))
(setq str_1 (substr string m na))
(if (= str_1 str)
(setq n m)
)
)
)
谢谢大家! 好像很复杂呢~ kyo90 发表于 2011-6-22 11:14 static/image/common/back.gif
好像很复杂呢~
;;这个可能简单些
;;(setq str "aa 10.2 b10x20.2")
;;(getNumberS str)返回("10.2" "10" "20.2")
;;======================================================
(defun getNumberS (str / ASC N STR1 STRN)
;;先将非数字用空格代替
(setq n 1)
(setq str1 "")
(repeat (strlen str)
(setq strn (substr str n 1))
(setq Asc (ascii strn))
(if (or (and (> Asc 47) (< Asc 58)) (= Asc 46))
(setq str1 (strcat str1 strn))
(setq str1 (strcat str1 " "))
)
(setq n (1+ n))
)
(strinSplitS str1)
)
;;这个可能简单些
;;提取字符串中数字
;;(setq str "aa 10.2 b10x20.2")
;;(getNumberA str)返回(10.2 10 20.2)
;;======================================================
(defun getNumberA (str / N STR1 STRN)
;;先将非数字用空格代替
(setq n 1)
(setq str1 "")
(repeat (strlen str)
(setq strn (substr str n 1))
(if (wcmatch strn "#,.")
(setq str1 (strcat str1 strn))
(setq str1 (strcat str1 " "))
)
(setq n (1+ n))
)
(strinSplitA str1)
)
;;; 学习了。。
页:
[1]
2