从字符串中删除某些内容
从字符串中删除[######.##.#如。
“蓝图.dwg”
移除时
“蓝图.dwg”
通常我们会使用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"
)
韩,你好,
非常感谢!
我想看看使用wcmatch方法。 使用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)
)
)
)
对不起,[#######.##可能不是两个,可能是一个或三个。 另一个,使用正则表达式:
(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"
啊,现在我明白了任务!在这里:
; _$ (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))
)
)
)
)
李,你的密码让我神魂颠倒! 谢谢Grrr
请注意,您的代码可能会变成:
这样,如果找不到模式,函数将返回原始字符串,而不是nil。
谢谢李,有时候我想得太多了,很难看出它可以缩短。 李,韩,Grrr,非常感谢!
页:
[1]