明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 492|回复: 6

[提问] (求助)字符串按数字分割输出表

[复制链接]
发表于 2022-11-8 15:52 | 显示全部楼层 |阅读模式
1明经币
请教,有任意字符串如

  1. ( setq str "CD61JY78K--45-A99SE" )
复制代码
拟返回
  1. ( CD 61 JY 78 K-- 45 -A 99 SE )
复制代码
已找到如下函数,可以去掉非数字字符,仅输入数字
  1. ( read ( strcat "("
  2.                    ( vl-list->string ( mapcar '( lambda ( x ) ( if ( <= 48 x 57 ) x 32 ) )
  3.                                                ( vl-string->list str )
  4.                                      )
  5.                    )
  6.                   ")"
  7.          )   
  8.   )
复制代码
是否可在此函数基础上修改,或者有什么别的好办法。我只能想到比较笨的办法,字符串转为字符表,用双指针挨个判断。

最佳答案

查看完整内容

;(xd::string:regexps "\\-*[A-Z]+\\-*|\\d+" "CD61JY78K--45-A99SE" "")->("CD" "61" "JY" "78" "K--" "45" "-A" "99" "SE") (defun XD::String:RegExpS (pat str key / end keys matches x) (if (not *xxvbsexp) (setq *xxvbsexp (vlax-get-or-create-object "VBScript.RegExp")) ) (vlax-put *xxvbsexp 'Pattern pat) (if (not key) (setq key "") ) (setq key (strcase key)) (setq keys '((" ...
发表于 2022-11-8 15:52 | 显示全部楼层
;(xd::string:regexps "\\-*[A-Z]+\\-*|\\d+" "CD61JY78K--45-A99SE" "")->("CD" "61" "JY" "78" "K--" "45" "-A" "99" "SE")
(defun XD::String:RegExpS (pat str key / end keys matches x)
  (if (not *xxvbsexp)
    (setq *xxvbsexp (vlax-get-or-create-object "VBScript.RegExp"))
  )
  (vlax-put *xxvbsexp 'Pattern pat)
  (if (not key)
    (setq key "")
  )
  (setq key (strcase key))
  (setq keys '(("I" "IgnoreCase") ("G" "Global")
         ("M" "Multiline")
        )
  )
  (mapcar
    '(lambda (x)
       (if (wcmatch key (strcat "*" (car x) "*"))
         (vlax-put *xxvbsexp (read (cadr x)) 0)
         (vlax-put *xxvbsexp (read (cadr x)) -1)
       )
     )
    keys
  )
  (setq matches (vlax-invoke *xxvbsexp 'Execute str))
  (vlax-for x matches (setq end (cons (vla-get-value x) end)))
  (reverse end)
)

点评

我的代码废了,就是觉得正则的简洁,有点伤心  发表于 2022-11-8 18:22

评分

参与人数 1金钱 +20 收起 理由
freedom_ice + 20 厉害,看不懂,还得学很久。

查看全部评分

回复

使用道具 举报

发表于 2022-11-8 16:24 | 显示全部楼层
你去学一下正则
回复

使用道具 举报

发表于 2022-11-8 16:37 | 显示全部楼层
试试
  1. ;;;=============================================
  2. ;;;      通用函数 将字符串分解成表
  3. ;;;参数:Str------字符串
  4. ;;;      dot------考虑小数t是nil否
  5. ;;;      oi-------t返回ascii表,nil字符串表
  6. ;;;返回值 ascii表组成的表
  7. ;;;备注:此函数针对含特殊字符串的
  8. (defun xty-str->list (str dot oi / tmp tmp1 tmp2 tmp3 a lst lst1 lst2)
  9.   (defun tmp (/ a)
  10.     (setq a   (car lst)
  11.     lst (cdr lst)
  12.     )
  13.     a
  14.     )
  15.   (defun tmp1 (str / lst a b lst1)
  16.     (setq lst (vl-string->list str))
  17.     (while lst
  18.       (setq a (tmp))
  19.       (if (< a 129)
  20.   (setq lst1 (cons (list a) lst1))
  21.   (setq b     (tmp)
  22.         lst1 (cons (list a b) lst1)
  23.         )
  24.   )
  25.       )
  26.     (reverse lst1)
  27.     )
  28.   (defun tmp2 (lst / typi typa a b c d e lst1 lsttmp)
  29.     (defun typi (a) (and (< 47 a) (< a 58)))
  30.     (defun typa  (a)
  31.       (or (and (< 64 a) (< a 91)) (and (< 96 a) (< a 123)))
  32.       )
  33.     (setq lsttmp lst
  34.     a   (car (tmp))
  35.     b   (car (tmp))
  36.     c   (car (tmp))
  37.     d   (car (tmp))
  38.     e   (car (tmp))
  39.     )
  40.     (cond
  41.       ((and (= 37 a) (= 37 b) (typi c) (typi d) (typi e))
  42.        (setq lst1   (list a b c d e)
  43.        lsttmp (cdr (cddddr lsttmp))
  44.        )
  45.        ) ;_符合%%###
  46.       ((or (and e (= 37 a) (= 37 b) (typi c) (typi d) (null (typi e)))
  47.      (and (= 37 a) (= 37 b) (typi c) (typi d) (null e))
  48.      )
  49.        (setq lst1   (list a b c d)
  50.        lsttmp (cddddr lsttmp)
  51.        )
  52.        ) ;_符合%%##
  53.       ((and (= 37 a) (= 37 b) (or (typa c) (typi c)))
  54.        (setq lst1   (list a b c)
  55.        lsttmp (cdddr lsttmp)
  56.        )
  57.        )
  58.       )
  59.     (list lst1 lsttmp)
  60.     )
  61.   (defun tmp3 (lst dot / typa a lst1 lst2)
  62.     (if  dot ;_是否考虑小数点
  63.       (defun typa (a) (and (< 45 a) (< a 58) (/= 47 a)))
  64.       (defun typa (a) (and (< 47 a) (< a 58)))
  65.       )
  66.     (setq lst2 nil
  67.     lst1 nil
  68.     )
  69.     (while (or lst lst2)
  70.       (setq a (tmp))
  71.       (if (typa (car a)) ;_是否数字
  72.   (setq lst2 (append a lst2)) ;_是,组lst2
  73.   (if lst2
  74.     (setq  lst1 (cons (reverse lst2) lst1)
  75.     lst1 (if a
  76.            (cons a lst1)
  77.            lst1
  78.            )
  79.     lst2 nil
  80.     )
  81.     (setq lst1 (cons a lst1))
  82.     )
  83.   )
  84.       )
  85.     (reverse lst1)
  86.     )
  87.   (setq lst (tmp1 str))
  88.   (while lst
  89.     (if  (car (setq lst2 (tmp2 lst)))
  90.       (setq lst1 (cons (car lst2) lst1)
  91.       lst   (cadr lst2)
  92.       )
  93.       (setq a   (tmp)
  94.       lst1 (cons a lst1)
  95.       )
  96.       )
  97.     )
  98.   (setq  lst (reverse lst1)
  99.   lst (tmp3 lst dot)
  100.   )
  101.   (if oi
  102.     lst
  103.     (mapcar 'vl-list->string lst)
  104.     )
  105.   )
  106. (defun tt (str / a lst)
  107.   (setq  lst nil
  108.   a   ""
  109.   )
  110.   (foreach n (xty-str->list str nil nil)
  111.     (if  (= 'INT (type (read n)))
  112.       (setq lst  (if (= a "")
  113.       (cons n lst)
  114.       (cons n (cons a lst))
  115.       )
  116.       a  ""
  117.       )
  118.       (setq a (strcat a n))
  119.       )
  120.     )
  121.   (setq  lst (if  (= a "")
  122.         (reverse lst)
  123.         (reverse (cons a lst))
  124.         )
  125.   )
  126.   )


评分

参与人数 1金钱 +20 收起 理由
freedom_ice + 20 感谢,我试试。

查看全部评分

回复

使用道具 举报

发表于 2022-11-8 17:02 | 显示全部楼层
本帖最后由 菜卷鱼 于 2022-11-8 17:05 编辑

负数要不要识别?
小数要不要识别?
下面是一段我已经废弃的代码,稍微拼了一下,目前来看跟你要的结果完全一致
如果你用得上,就没有枉费我当时的一番功夫
(setq str "CD61JY78K--45-A99SE" )
(splitnums str)
===>
("CD" "61" "JY" "78" "K--" "45" "-A" "99" "SE")

  1. (defun splitnums (str / s a)
  2.   (setq s (spnum str))
  3.   (setq a (car s))
  4.   (while (wcmatch a "*#*")
  5.     (setq s (append (spnum a) (cdr s)))
  6.     (setq a (car s))
  7.   )
  8.   s
  9. )
  10. (defun spnum (str / spl pre stb num)
  11.   (if (wcmatch str "*#*")
  12.     (progn
  13.       (setq spl (split_str_last str))
  14.       (setq pre  (car spl)
  15.       stb  (cadr spl)
  16.       )
  17.       (setq spl (split_num_last pre))
  18.       (setq pre  (car spl)
  19.       num  (cadr spl)
  20.       )
  21.       (if (wcmatch pre "*#`.")
  22.   (progn
  23.     (setq spl (split_last pre))
  24.     (setq  pre (car spl)
  25.     num (strcat "." num)
  26.     )
  27.     (setq spl (split_num_last pre))
  28.     (setq  pre (car spl)
  29.     num (strcat (cadr spl) num)
  30.     )
  31.   )
  32.       )
  33.       (setq spl (split_num_real num))
  34.       (setq pre  (strcat pre (car spl))
  35.       num  (cadr spl)
  36.       )
  37. ;;;;;以下句是为了保留负数
  38.       ;|(if (wcmatch pre "*-")
  39.   (setq pre (car (split_last pre))
  40.         num (strcat "-" num)
  41.   )
  42.       )|;
  43.       (list pre num stb)
  44.     )
  45.     (list str "" "")
  46.   )
  47. )
  48. ;;;;分出尾部的字符串
  49. (defun split_str_last (str / n l)
  50.   (setq n (strlen str))
  51.   (setq l n)
  52.   (while
  53.     (and (> n 0)
  54.    (wcmatch (substr str n 1) "~[0-9]")
  55.     )
  56.      (setq n (1- n))
  57.   )
  58.   (cond
  59.     ((= n l) (list str ""))
  60.     ((< 1 n l) (list (substr str 1 n) (substr str (1+ n))))
  61.     ((<= n 1) (list "" str))
  62.   )
  63. )
  64. ;;;;分出尾部的数字
  65. (defun split_num_last (str / n l)
  66.   (setq n (strlen str))
  67.   (setq l n)
  68.   (while
  69.     (and (> n 0)
  70.    (wcmatch (substr str n 1) "[0-9]")
  71.     )
  72.      (setq n (1- n))
  73.   )
  74.   (cond
  75.     ((= n l) (list str ""))
  76.     ((<= 1 n l) (list (substr str 1 n) (substr str (1+ n))))
  77.     ((< n 1) (list "" str))
  78.   )
  79. )
  80. (defun split_last (str / n)
  81.   (setq n (strlen str))
  82.   (if (> n 1)
  83.     (list (substr str 1 (1- n)) (substr str n))
  84.     (list "" str)
  85.   )
  86. )
  87. (defun split_num_real (num / n n2str pos)
  88.   (setq n (read num))
  89.   (setq n2str (vl-princ-to-string n))
  90.   (setq pos (vl-string-search n2str num))
  91.   (if (zerop pos)
  92.     (list "" num)
  93.     (list (substr num 1 pos) (substr num (1+ pos)))
  94.   )
  95. )
  96. (defun strnum-prc (str / pos)
  97.   (setq pos (vl-string-search "." str))
  98.   (if (null pos)
  99.     0
  100.     (- (strlen str) (1+ pos))
  101.   )
  102. )



评分

参与人数 1明经币 +1 收起 理由
freedom_ice + 1 感谢,我就是需要正则或者字符匹配之类的拆.

查看全部评分

回复

使用道具 举报

发表于 2023-9-23 13:16 | 显示全部楼层
感谢大佬分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-8 14:14 , Processed in 0.280396 second(s), 29 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表