分析字符串
你好全部的如果我想在一个大字符串中获取/覆盖一些特殊的子字符串。做这件事最有效/最容易的是什么。
例如
我有一个字符串{\H1.1429x;φ}{\H0.7x;\S+0.1^-0.2;\H1.4286x;\C1;(小)}
然后我想得到蓝色部分的子串。或者用“替换”覆盖蓝色部分,什么
我应该这样做吗?谢谢 可能使用正则表达式 @李。谢谢,我会深入研究的。 嗨,李
经过一些研究,我使用以下代码来获得值“+0.1”和“-0.2”,但它失败了,有任何评论吗??
(defun c:tt(/)
(vl-load-com)
(setq regex (vlax-create-object "Vbscript.RegExp"))
(vlax-put-property regex "IgnoreCase" 0)
(vlax-put-property regex "Global" 1)
(vlax-put-property regex "Pattern" "\\\\S(.*)(\\^|#|\\\\)(.*);")
(setq string "{\H1.1429x;φ}<>{\H0.7x;\S+0.1^-0.2;\H1.4286x;\C1;(small)}")
(setq result (vlax-invoke-method regex "Execute" string))
(vlax-for x result
(setq match (vlax-get x 'Value)
idx(valx-get x 'FirstIndex)
lst (cons (list match idx) lst)
)
)
lst
)
好的,这是令人惊讶的代码。
但它返回匹配的字符串\\S+0.1^-0.2;
如果我只想得到“+0.1”和“-0.2”,我该怎么办?
(defun c:tt(/ regex lst)
(vl-load-com)
(setq regex (vlax-create-object "Vbscript.RegExp"))
(vlax-put-property regex "IgnoreCase" 0)
(vlax-put-property regex "Global" 1)
(vlax-put-property regex "Pattern" "\\\\S(.[^;]*)(\\^)(.[^;]*);")
(setq string "{\H1.1429x;φ}<>{\H0.7x;\\S+0.1^-0.2;\H1.4286x;\C1;(small)}")
(setq result (vlax-invoke-method regex "Execute" string))
(vlax-for x result
(setq match (vlax-get x 'Value)
idx(vlax-get x 'FirstIndex)
lst (cons (list match idx) lst)
)
)
lst
(vlax-release-object regex)
) 我不太擅长常规表达,但也许这更接近?
(defun c:tt ( / *error* regex str )
(vl-load-com)
(setq str "{\H1.1429x;?}<>{\H0.7x;\\S+0.1^-0.2;\H1.4286x;\C1;(small)}")
(defun *error* ( msg )
(LM:ReleaseObject regex)
(or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
(princ (strcat "\n** Error: " msg " **")))
(princ)
)
(setq regex (vlax-create-object "VBScript.RegExp"))
(setq str (caar (LM:RegExExecute regex "\\\\S.[^;]*\\^.[^;]*;" str)))
(print (LM:RegExExecute regex "[^S]*\\^[^;]*" str))
(LM:ReleaseObject regex)
(princ)
)
(defun LM:RegExExecute ( regex pat str / l )
;; © Lee Mac 2010
(mapcar
(function
(lambda ( prop value ) (vlax-put-property regex prop value))
)
'(pattern global ignorecase) (list pat actrue acfalse)
)
(vlax-for x (vlax-invoke regex 'execute str)
(setq l (cons (list (vlax-get x 'value) (vlax-get x 'firstindex)) l))
)
(reverse l)
)
;;------------------=={ Release Object }==--------------------;;
;; ;;
;;Releases a VLA Object from memory via plentiful error ;;
;;trapping ;;
;;------------------------------------------------------------;;
;;Author: Lee McDonnell, 2010 ;;
;; ;;
;;Copyright © 2010 by Lee McDonnell, All Rights Reserved. ;;
;;Contact: Lee Mac @ TheSwamp.org, CADTutor.net ;;
;;------------------------------------------------------------;;
;;Arguments: ;;
;;obj - VLA Object to be released from memory ;;
;;------------------------------------------------------------;;
;;Returns:T if Object Released, else nil ;;
;;------------------------------------------------------------;;
(defun LM:ReleaseObject ( obj )
(vl-load-com)
;; © Lee Mac 2010
(and obj (eq 'VLA-OBJECT (type obj)) (not (vlax-object-released-p obj))
(not
(vl-catch-all-error-p
(vl-catch-all-apply
(function vlax-release-object) (list obj)
)
)
)
)
) 最后一个。
(defun c:tt ( / *error* regex str )
(vl-load-com)
(setq str "{\H1.1429x;?}<>{\H0.7x;\\S+0.1^-0.2;\H1.4286x;\C1;(small)}")
(defun *error* ( msg )
(LM:ReleaseObject regex)
(or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
(princ (strcat "\n** Error: " msg " **")))
(princ)
)
(setq regex (vlax-create-object "VBScript.RegExp"))
(setq str (caar (LM:RegExExecute regex "\\\\S.[^;]*\\^.[^;]*;" str)))
(setq str (caar (LM:RegExExecute regex "[^S]*\\^[^;]*" str)))
(print (LM:RegExExecute regex "[^\\^]+" str))
(LM:ReleaseObject regex)
(princ)
)
(defun LM:RegExExecute ( regex pat str / l )
;; ? Lee Mac 2010
(mapcar
(function
(lambda ( prop value ) (vlax-put-property regex prop value))
)
'(pattern global ignorecase) (list pat actrue acfalse)
)
(vlax-for x (vlax-invoke regex 'execute str)
(setq l (cons (list (vlax-get x 'value) (vlax-get x 'firstindex)) l))
)
(reverse l)
)
;;------------------=={ Release Object }==--------------------;;
;; ;;
;;Releases a VLA Object from memory via plentiful error ;;
;;trapping ;;
;;------------------------------------------------------------;;
;;Author: Lee McDonnell, 2010 ;;
;; ;;
;;Copyright ? 2010 by Lee McDonnell, All Rights Reserved. ;;
;;Contact: Lee Mac @ TheSwamp.org, CADTutor.net ;;
;;------------------------------------------------------------;;
;;Arguments: ;;
;;obj - VLA Object to be released from memory ;;
;;------------------------------------------------------------;;
;;Returns:T if Object Released, else nil ;;
;;------------------------------------------------------------;;
(defun LM:ReleaseObject ( obj )
(vl-load-com)
;; ? Lee Mac 2010
(and obj (eq 'VLA-OBJECT (type obj)) (not (vlax-object-released-p obj))
(not
(vl-catch-all-error-p
(vl-catch-all-apply
(function vlax-release-object) (list obj)
)
)
)
)
) 很高兴你最后到了那里
不过,它可能需要更多的错误捕捉。。。 嗨,李:
我找到了另一种更好的方法来获得价值。
代码如下。
(defun c:tt(/ regex lst)
(vl-load-com)
(setq regex (vlax-create-object "Vbscript.RegExp"))
(vlax-put-property regex "IgnoreCase" 0)
(vlax-put-property regex "Global" 1)
;
(vlax-put-property regex "Pattern" ".*\\\\S(.[^;]*)[\\^](.[^;]*);.*")
(setq string "<>{\H0.7x;\\S+0.3^+0.5;}")
(setq tp (vlax-invoke-method regex "Replace" string "$1"))
(setq tm (vlax-invoke-method regex "Replace" string "$2"))
(princ tp)
(princ tm)
(vlax-release-object regex)
)
基于同一个故事。
但它未能满足以下代码。你知道这个吗???
(defun c:tt(/ regex lst)
(vl-load-com)
(setq regex (vlax-create-object "Vbscript.RegExp"))
(vlax-put-property regex "IgnoreCase" 0)
(vlax-put-property regex "Global" 1)
;
(vlax-put-property regex "Pattern" ".*%%P(\d*[\\\\.]?\d*).*")
(setq string "<>%%P0.05")
(setq tp (vlax-invoke-method regex "Replace" string "$1"))
;(setq tm (vlax-invoke-method regex "Replace" string "$2"))
(princ tp)
;(princ tm)
(vlax-release-object regex)
)
页:
[1]
2