明27662 发表于 2020-8-28 22:49:02

如何把字符串中的几个数字取出来?

“{\\H1.75x; 筒体 %%C2000*12}L=3500”

如何把字符串中的200012   3500这几个数字取出来?

muwind 发表于 2020-8-29 22:24:30

以下连接可能能帮助你,如果不用正则的话需要先清除格式再获取获取数字
用正则的化 我也搞不定正则,所以只能帮你到这里了
http://bbs.mjtd.com/forum.php?mod=viewthread&tid=110663&fromuid=399892
http://bbs.mjtd.com/forum.php?mod=viewthread&tid=170039&extra=&highlight=%D7%D6%B7%FB%B4%AE&page=2
http://bbs.xdcad.net/forum.php?mod=viewthread&tid=704546&highlight=%C7%E5%B3%FD%2B%B8%F1%CA%BD

自贡黄明儒 发表于 2020-8-30 08:08:39

这个比较难,晓东老大亲自出马解决这个问题,也是我见过的最好的

ysq101 发表于 2020-9-18 00:27:29

自贡黄明儒 发表于 2020-8-30 08:08
这个比较难,晓东老大亲自出马解决这个问题,也是我见过的最好的

这个问题对对你等大佬级也有难度吗?
以下是我收录的一段数字求和 的代码

本人愚见:
既然可以求和,那就应该是分别提取了各个数值部分   (但实测楼主那些字符串,发现有*号,所以求和失败)

所以能不能将这段代码优化一下??

PS:我只是抛砖   具体我等学渣也实在没办法了
希望对你有所帮助



ysq101 发表于 2020-9-18 00:27:41

;|(xtcal)= 文本计数求和;----------------------------------------------------------------陌生人.2004.1
;v1.1 2004.1.对mtext的bug修正。消除重复符号;支持-.5写法,排除"写.法" ".." "+-";
功能:对选集中文本进行所有数字计算,支持一个text,mtext中有多个数字字符串,支持字符串中小数,负数:
返回: 有数字,数字相加后写文本,并返回求和数值(非字符串).无有效数字返回nil.
|;
(defun xtcal ( / ss filter mspace n e str asclst strs add pt txt txth)
(defun *error* (msg) (if ss (x_draw ss 4)) (setq *error* oerr))
(princ "\n文本数字求和-----------------------陌生人.2004.1")
(princ "\n选择要计算的文本(支持*TEXT选择集):")
(setq oerr *error*
      ss (ssget '((0 . "*TEXT")))
      filter "0123456789.-+"
      mspace (vla-get-modelspace(vla-get-activedocument (vlax-get-acad-object)))
      str nil strs nil)
(if ss
    (repeat (setq n (sslength ss))
      (x_draw ss 3)
      (setq n (1- n)
            e (ssname ss n)
            str (vla-get-textstring(vlax-ename->vla-object e))
            strs (strcat (if strs strs " ") (x_txt2 str) " ")) ;;排除mtext bug.v1.1-2004.1
      )
    )
(if (and ss (/= "" strs))
    (progn
      (setq add (eval (read (strcat "(+ " strs ")"))))
      (princ "\n文本数字和为: ")(princ add)
      (if (setq pt (getpoint "\n标注位置<重新计算>:"))
          (progn
            (setq prec 2
                  txt (rtos add 2 prec)
                  txth 8)
            (vla-addtext mspace txt (vlax-3D-point pt) txth)
            (x_draw ss 4)
            (princ) add)
          (progn (if ss (x_draw ss 4))(xtcal));多次<重新计算>可以作为一个简易统计查看器.
      )
    )
    (progn (princ "\n!空选集或文本中无有效数字!\n") nil)
)
)
;;
(defun x_draw (ss key / n e)
(if (= 'PICKSET (type ss))
    (repeat (setq n (sslength ss))
      (setq n (1- n)
            e (ssname ss n))
      (redraw e key)
    )
)
)
;;
(defun x_txt2 (str / i key str1)
(setq i 1)
(repeat (strlen str)
   (cond
   ((= "{\\f" (substr str i 3)) (setq i (+ 3 i) key T))
   ((and T (= "}" (substr str i 1))) (setq key nil))   
   ((not key)
      (setq st (substr str i 1)
            str1 (strcat (if (not str1) "" str1)
      (cond ((= "." st)(if (wcmatch (substr str (1+ i) 1) "#") st " "))
                               ((member st '("+" "-")) (if (wcmatch (substr str (1+ i) 1) "#,'.") st " "))
                               (T (if (wcmatch filter (strcat "*" st "*")) st " "))
                         )
                         )))
   )
   (setq i (1+ i))
)
(setq str str1)
)

(defun c:JJ ( / )
(xtcal)
)

天宇飞翔12 发表于 2022-2-7 12:15:51

本帖最后由 天宇飞翔12 于 2022-2-7 12:17 编辑

5楼的高手,请问字符串里有中文怎么办?比如"外保温:30kN",需要提取30参与求和
页: [1]
查看完整版本: 如何把字符串中的几个数字取出来?