[解决]类array的vla对象怎样取得任意子项而不是枚举
本帖最后由 CAD新军 于 2019-5-12 11:39 编辑_$ (vlax-dump-object matches)
; IMatches: nil
;特性值:
; Count (RO) = 4
; Item (RO) = ...不显示带索引的内容...
; _NewEnum (RO) = #<IUnknown 000000004dda9ca0>
T
_$ 我现在在编的一个小脚本,大量代码需要用到正则表达式,网上找了几个vbs实现的,用着都发现有些小问题,想改进一下时,第一次碰vlax对象,遇到了问题.
一个正则返回对象 matches在VBS中时这样一个Array : [ Matches(0) Matches(1) .... Matches (n) ]
而每个Matches(n) 也是一个Array ,形如 [ FirstIndex Value Submatches ]
VBS里面就直接 Matches(3) 就能取到第4个。而我找到的函数都是用 (vlax-for m matches (vlax-get m 'Value)) 来取得各个值的,我想问有没有类似(nth n list)的方法,直接取得某个Array里面的值呢?
我其实用的是Submatches数组里面的值,但问题是 vlax-for 遍历 submatches时,遇到空的数组会直接出错终止循环。比如这个正则表达式:
(1)(a)?(2)?
当匹配 1a2时, 三个捕获都会得到, 1,a,2
但匹配 12时,因为第二个捕获没有命中,只会返回 1,而无视后面的捕获,这就出问题了
精简出来的复现代码:
(defun RegExpSet (pattern / regex)
(setq regex (vlax-create-object "VBScript.RegExp"))
(vlax-put regex 'Pattern pattern)
(vlax-put regex 'IgnoreCase acTrue)
(vlax-put regex 'Global acTrue)
regex
)
(setq slist nil
str "13"); ############这里改成"1a3"就会正常不出错误
(setq ms (vlax-invoke (RegExpSet "()()?()?" ) 'Execute str))
(vlax-for m ms (setq jj m))
; 这句其实就是标题的问题,我只想要ms(0),因为不会直接读取 ms(0)只好用vlax-for
;这里组里只有一个值,用vlax-for的办法读出来,记成 jj, 现在jj 相当于 Matches(0)
(setq sub (vlax-get jj 'SubMatches))
; sub 就相当于 Matches(0).SubMatches, 这也是一个Array,包含所有捕获字符串分组
(vlax-for s sub (setq slist (cons s slist)))
; 获得每个submatches的值,合并到slist 表中
;同理这也是标题中的问题,我只想要某一段捕获比如submatches(2),但又只好用vlax-for
;;直接粘贴的话,运行到上面一句会出错...str改成1a3就能整体完成
(princ slist);可以看到用"13"去匹配,会先是1然后就没有了,如果用"1a3"去匹配会正确返回1 a 3
(vlax-dump-object sub);这里就是开头的情况了
(vlax-dump-object sub) 获取的是属性列表,不含有方法,当然这个对象本身也没有方法。
_1$ (vlax-dump-object sub t)
; ISubMatches: nil
; Property values:
; Count (RO) = 3
; Item (RO) = ...Indexed contents not shown...
; _NewEnum (RO) = #<IUnknown 000001338f19d790>
; No methods
T
访问对象的属性用(vlax-get-property object property)那我们来试试是否可行
_$ (vlax-get-property sub 'item)
; error: too few actual parameters
_1$
发现返回错误,提示参数太少,根据改对象的属性,有一个count数量,而item属性提示了Indexed ,那么我们来猜测是否是增加一个序号
_1$ (vlax-get-property sub 'item 0)
#<variant 8 1>
_1$
看来返回是成功的,接着尝试获取这个变体的值。
_1$ (vlax-variant-value (vlax-get-property sub 'item 0))
"1"
我们可以依次获取后面的序号的值。
_1$ (vlax-variant-value (vlax-get-property sub 'item 1))
nil
_1$ (vlax-variant-value (vlax-get-property sub 'item 2))
"3"
vlax-for的问题我已经解决了,找到一个不会跳出循环的方法。标题的问题,就是vla数组对象怎么直接选第n个元素还没有方法 晓东也发了,里面提到(vla-item 的方法,搜索了一下好像确实是这个用途的,无奈对正则的返回对象好像无法操作?或者我用的不对 前面的ms变量也可以用同样的方式查询以及猜测。
_$ (vlax-dump-object ms t)
; IMatchCollection2: nil
; Property values:
; Count (RO) = 1
; Item (RO) = ...Indexed contents not shown...
; _NewEnum (RO) = #<IUnknown 000001338f19d370>
; No methods
T
返回属性数量1,有item属性。那么同理试试。。
_1$ (vlax-get-property ms 'item 0)
#<VLA-OBJECT IMatch2 000001338c09ad80>
_1$
提示返回vla-object对象,所以对象可以直接使用了,无需vlax-for,vlax-for是对集合遍历,应该没有办法退出,等同于lisp中的repeat。 本帖最后由 CAD新军 于 2019-5-12 11:42 编辑
edata 发表于 2019-5-12 09:50
前面的ms变量也可以用同样的方式查询以及猜测。
返回属性数量1,有item属性。那么同理试试。。
感谢啊。我怕没人回答还卖了个关子的。
上面说vlax-for遇到未定义对象时会出错跳出应该是cad的bug了,但在autodesk的开发资料里面发现一个方法,
vlax-map-collection,这个方法遇到未定义对象返回的是nil,可以继续循环的,改进的正则表达式如下
;; RegExpSet
;; Returns the current VBScript.RegExp instance after defining its properties.
;;
;; Arguments
;; pattern : Pattern to search.
;; ignoreCase : If non nil, the search is done ignoring the case.
;; global : If non nil, search all occurences of the pattern;
;; if nil, only searches the first occurence.
(defun RegExpSet (pattern ignoreCase global / regex)
(setq regex
(cond
((vl-bb-ref '*regexp*))
((vl-bb-set '*regexp* (vlax-create-object "VBScript.RegExp")))
)
)
(vlax-put regex 'Pattern pattern)
(if ignoreCase
(vlax-put regex 'IgnoreCase acTrue)
(vlax-put regex 'IgnoreCase acFalse)
)
(if global
(vlax-put regex 'Global acTrue)
(vlax-put regex 'Global acFalse)
)
regex
)
;; RegExpExecute
;; Returns the list of matches with the pattern found in the string.
;; Each match is returned as a sub-list containing:
;; - the match value
;; - the index of the first character (0 based)
;; - a list of sub-groups.
;;
;; Arguments
;; string : String in which the pattern is searched.
;; pattern : Pattern to search.
;; ignoreCase : If non nil, the search is done ignoring the case.
;; global : If non nil, search all occurences of the pattern;
;; if nil, only searches the first occurence.
;;
;; Examples
;; (RegExpExecute "foo bar baz" "ba" nil nil) ; => (("ba" 4 nil))
;; (RegexpExecute "12B 4bis" "(+)(+)" T T) ; => (("12B" 0 ("12" "B")) ("4bis" 4 ("4" "bis")))
;; (RegexpExecute "-12 25.4" "(-?\\d+(?:\\.\\d+)?)" nil T); => (("-12" 0 ("-12")) ("25.4" 4 ("25.4")))
(defun RegExpExecute2 (string pattern ignoreCase global / sublst lst)
(vlax-for match (vlax-invoke (RegExpSet pattern ignoreCase global) 'Execute string)
(setq sublst nil)
(vl-catch-all-apply
'(lambda ()
(vlax-map-collection(vlax-get match 'SubMatches)
;(if submatch
;(setq sublst (cons submatch sublst))
;)
'(lambda(str)(setq sublst (cons str sublst)))
)
)
)
(setq lst (cons (list (vlax-get match 'Value)
(vlax-get match 'FirstIndex)
(reverse sublst)
)
lst
)
)
)
(reverse lst)
)
由于我的用途需要,要借助每一个捕获字符串定位,所以输出所有捕获串,无论是否有捕获(这也符合其它正则语言的返回结果)
(regexpexecute2 "1a3" "(1)?(a)?(3)?" nil nil)
;返回 ("1a3" 0 ("1" "a" "3"))
(regexpexecute2 "13" "(1)?(a)?(3)?" nil nil)
;返回 ("13" 0 ("1" nil "3")) edata 发表于 2019-5-12 09:50
前面的ms变量也可以用同样的方式查询以及猜测。
返回属性数量1,有item属性。那么同理试试。。
另外前辈我想请教一下,你的CAD是英文版吗?我的vlisp控制台出来的也有一些汉化了,让我很难借助谷歌搜索英文结果,比如 ...不显示带索引的内容... ...Indexed contents not shown...换成英文我能看懂,但写成中文要搜索英文结果就难了 是英文版。
页:
[1]