DuanJinHui 发表于 2022-7-5 16:34:26

从字符串中删除某些内容

从字符串中删除[######.##.#
 
如。
“蓝图.dwg”
 
移除时
 
“蓝图.dwg”

hanhphuc 发表于 2022-7-5 16:46:18

 
通常我们会使用wcmatch,
或者,我使用列表方法bcos,因为我看到了括号[##]
 


(setq str "blueprint.dwg")

(strcat        (vl-string-right-trim
" "
(apply 'strcat
       (mapcar ''((x) (strcat (vl-princ-to-string x) " "))
               (vl-remove-if 'vl-consp (read (strcat "(" (vl-string-translate "[]" "()" str) ")")))
               )
       )
)
".dwg"
)


DuanJinHui 发表于 2022-7-5 16:54:13

 
韩,你好,
非常感谢!
我想看看使用wcmatch方法。

Grrr 发表于 2022-7-5 17:00:25

使用wcmatch:

; _$ (foo "Blueprint.dwg") -> "Blueprint.dwg"
; _$ (foo "This is a test.dwg") -> "This is a test.dwg"
(defun foo ( s )
(cond
   ( (wcmatch s"`[####`.##`.##`]`[####`.##`.##`]*`.dwg")
   (substr s 25)
   )
)
)

DuanJinHui 发表于 2022-7-5 17:01:31

 
对不起,[#######.##可能不是两个,可能是一个或三个。

Lee Mac 发表于 2022-7-5 17:10:14

另一个,使用正则表达式:
(defun foo ( str / rgx )
   (if (setq rgx (vlax-get-or-create-object "vbscript.regexp"))
       (progn
         (vlax-put-property rgx 'global actrue)
         (vlax-put-property rgx 'pattern "\\[\\d{4}\\.\\d{2}\\.\\d{2}\\]")
         (setq str (vlax-invoke rgx 'replace str ""))
         (vlax-release-object rgx)
         str
       )
   )
)
_$ (foo "Blueprint.dwg")
"Blueprint.dwg"
_$ (foo "Blueprint.dwg")
"Blueprint.dwg"
_$ (foo "Blueprint.dwg")
"Blueprint.dwg"

Grrr 发表于 2022-7-5 17:15:21

 
啊,现在我明白了任务!在这里:

; _$ (foo "Blueprint.dwg") -> "Blueprint.dwg"
; _$ (foo "This is a test.dwg") -> "This is a test.dwg"
; _$ (foo "And Another test.dwg") -> "And Another test.dwg"
(defun foo ( s )
(cond
   ( (wcmatch s"`[####`.##`.##`]*`.dwg")
   (while (wcmatch s"`[####`.##`.##`]*`.dwg")
       (setq s (substr s 13))
   )
   )
)
)
 
李,你的密码让我神魂颠倒!

Lee Mac 发表于 2022-7-5 17:22:56

谢谢Grrr
 
请注意,您的代码可能会变成:
这样,如果找不到模式,函数将返回原始字符串,而不是nil。

Grrr 发表于 2022-7-5 17:34:19

 
谢谢李,有时候我想得太多了,很难看出它可以缩短。

DuanJinHui 发表于 2022-7-5 17:37:30

李,韩,Grrr,非常感谢!
页: [1]
查看完整版本: 从字符串中删除某些内容