gpt4 book ai didi

vba - 多点正则表达式匹配较少的 for 循环和较少的类重新初始化。优化

转载 作者:行者123 更新时间:2023-12-04 22:10:04 25 4
gpt4 key购买 nike

有没有办法在 vba 中进行多个正则表达式匹配而不实例化 regexp 对象的多个实例?例如我想在几个不同的数据字段之间做几个点的正则表达式匹配。

出于说明目的考虑这一点。

 column 1       column 2         column 3AAATDD         Airplane         Transportation/AirplaneBBBTDD         Bus              Transportation/BusCCCFDD         Chocolate        Food/ChocolateDDDFDD         Dog              Food/Potato

So as you can see the first letter in Column 1 is A, which matches the Description in Column 2 for Airplane which matches the type of Transportation (indicated by the T in Column 1 "AAATDD") in Column 3 for Airplane. All is well. But in Column 1 there is the peculiar entry for Dog which is listed as a Food and Potato.

Right now I have (pseudo) code like this:

for i = 1 to endpoint
set regexp as new regexp
regexp.pattern is myPattern
set colmatches = regexp.execute()

If Column1_Entry(i) = myMatch.value then
pass test
Else
throw exception
End if
next i

但问题是我只能检查我的模式给出的正则表达式。例如,我接下来的几行必须检查 SAME 值只是为了寻找不同的东西。

当我准备好进行下一组匹配以确定有效性时,即在第 3 列将运输/“飞机”与第 2 列中的“飞机”匹配时,我需要取消设置正则表达式并重置 myPattern。有没有办法解决?还是一种将正则表达式存储为列表或数组的方法?那么它们可以依次循环吗?任何帮助将不胜感激。

最佳答案

Dim arr

arr = Array("pattern1","pattern2","pattern3")

set regexp as new regexp

for i = 1 to endpoint
for n = lbound(arr) to ubound(arr)
regexp.pattern = arr(n)
set colmatches = regexp.execute()
If Column1_Entry(i) = myMatch.value then
pass test
Else
throw exception
End if
next n
next i

关于vba - 多点正则表达式匹配较少的 for 循环和较少的类重新初始化。优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9672436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com