本帖最后由 作者 于 2007-1-15 21:27:38 编辑
这简直是太简单了,因为正则表达式就是专门为这一类的替换设计的: (defun c:replacetxt(/ regex s txtstring) (setq regex(vlax-create-object "Vbscript.RegExp"));引用正则表达式控件 (vlax-put-property regex "IgnoreCase" 0); 不忽略大小写 (vlax-put-property regex "Global" 1);全文匹配,而不是只匹配第一处 (setq txtstring "A1BCD A1BBD A1BDD ") (setq s txtstring) ;替换字符 (vlax-put-property regex "Pattern" "A1(.{2})D") (setq s(vlax-invoke-method regex "Replace" s "A2$1E")) (vlax-release-object regex) (princ (strcat "\n替换前为:" txtstring)) (princ (strcat "\n替换后为:" s)) (princ) ) 程序运行结果: 命令: REPLACETXT 替换前为:A1BCD A1BBD A1BDD 替换后为:A2BCE A2BBE A2BDE
|