遍历匹配项并替换 VBA RegEx
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14000054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iterate through matches and replace for VBA RegEx
提问by Jon49
Is it possible to iterate through a VBA RegEx
matches and replace with specific data given by an ID value?
是否可以遍历VBA RegEx
匹配项并替换为 ID 值给出的特定数据?
E.g.,
例如,
<a id="a-UP:124" {REPLACEITEMHERE}
.../a>
with my pattern something like this:
我的模式是这样的:
<a id="a-([\w\d]+:[\w\d]+)" ({REPLACEITEMHERE})
So I have multiple "replace items" each being unique to the value of UP:124
.
所以我有多个“替换项目”,每个项目对于UP:124
.
Is this possible in VBA RegEx
? Just wanted to know before I go through a more cumbersome process! Thanks!
这可能VBA RegEx
吗?只是想在我经历一个更繁琐的过程之前知道!谢谢!
UPDATE (More details per the request of the commenters - hope this makes it more clear what I'm looking for! I understand how to create the patterns it is more iterating through the results and then performing a replace on each find that I am having trouble with. Thanks!):
更新(根据评论者的要求提供更多详细信息 - 希望这能让我更清楚地了解我在寻找什么!我了解如何创建模式,它更遍历结果,然后对我遇到的每个发现执行替换麻烦。谢谢!):
This is the RegEx pattern that I am using:
这是我正在使用的 RegEx 模式:
<a id="a-([\w\d]+:[\w\d]+)"[^{]+({FILE})[^{]+({PERCENT})[^{]+({COLOR})
With the settings as:
设置为:
.Global = True
.IgnoreCase = True
.MultiLine = False
The replace pattern I would like is to check what the value of the first capture group $1
is and then replace the values {FILE}
{PERCENT}
{COLOR}
(groups $2
, $3
, and $4
) with the appropriate values that I have stored in a class.
替换模式,我想是检查什么第一个捕获组的值$1
是,然后替换值{FILE}
{PERCENT}
{COLOR}
(组$2
,$3
和$4
)用适当的值,我存储在一个类中。
<path
style="fill:#d40000;fill-opacity:1;filter:url(#filter5248)"
d="m 168.04373,162.08375 c -4.7586,-5.00473 -8.65201,-9.35811 -8.65201,-9.67419 0,-0.81973 18.30811,-16.3921 25.16949,-21.40847 7.11903,-5.20474 16.462,-10.93031 17.83606,-10.93031 0.56369,0 3.81291,5.56174 7.22048,12.35942 l 6.19558,12.35941 -7.13301,3.9009 c -7.96536,4.3561 -21.53264,13.83148 -27.5305,19.22729 -2.16466,1.94738 -4.05237,3.47876 -4.19491,3.40307 -0.14254,-0.0757 -4.15257,-4.2324 -8.91118,-9.23712 z"
id="path5246"
inkscape:connector-curvature="0"
transform="matrix(0.8,0,0,-0.8,0,792)" />
<a id="a-UP:115E"
xlink:href="{FILE}"
xlink:title="UP:115E
{PERCENT}%">
<path
id="UP:115E"
style="fill:{COLOR};fill-opacity:1;stroke:none"
d="m 272.81031,529.10942 c 0.32799,18.973 -0.6558,38.48935 0.49159,57.12295 13.02609,-0.33792 26.60749,0.66479 39.29456,-0.4916 -0.32799,-18.973 0.6558,-38.48935 -0.49159,-57.12294 -13.01823,0.33523 -26.61862,-0.66099 -39.29456,0.49159 z"
inkscape:connector-curvature="0"
transform="matrix(0.8,0,0,-0.8,0,792)" />
</a>
<a id="a-UP:115D"
xlink:href="{FILE}"
xlink:title="UP:115D
{PERCENT}%">
<path
id="UP:115D"
style="fill:{COLOR};fill-opacity:1;stroke:none"
d="m 314.75946,529.10942 c 0.32799,18.973 -0.6558,38.48935 0.4916,57.12295 9.11694,0.926 18.85965,-1.04961 27.69299,0.721 -0.31086,4.08011 6.71077,4.04524 8.35706,1.67141 -0.0756,-1.75206 -3.96676,-2.62149 0,-2.32687 8.75271,2.70871 7.9153,-4.7371 7.43942,-11.04442 -0.32811,-15.47719 0.65596,-31.4979 -0.49159,-46.63566 -14.41803,0.33385 -29.41334,-0.65954 -43.48948,0.49159 z"
inkscape:connector-curvature="0"
transform="matrix(0.8,0,0,-0.8,0,792)" />
</a>
</g></svg>
采纳答案by brettdj
I think you could do something like this simplified example (given you input string was difficult to set-up to test)
我认为你可以做类似这个简化示例的事情(假设你输入的字符串很难设置来测试)
- Turn
Global
toFalse
to use a singleRegexp
for each replacement (the alternative is to useGlobal = True
but then run variousRegexps
for different first matches) - Use a
Do
loop to test whether a validRegexp
remains - Test the first
submatch
, and then useSelect Case
to run different routines to replacesubmatches $2-$4
(which I stored in a simple array)
- 转动
Global
到False
使用单个Regexp
用于每个替换(的替代方法是使用Global = True
但随后运行各种Regexps
不同的第一匹配) - 使用
Do
循环来测试一个有效的是否Regexp
仍然存在 - 测试第一个
submatch
,然后使用Select Case
运行不同的例程来替换submatches $2-$4
(我存储在一个简单的数组中)
code
代码
Sub TestSub()
Dim strIn As String
Dim objRegex As Object
Dim objRegMC As Object
Dim objRegM As Object
Dim vArray(1 To 3, 1 To 2)
vArray(1, 1) = "No 1a"
vArray(2, 1) = "No 2a"
vArray(3, 1) = "No 3a"
vArray(1, 2) = "number 1b"
vArray(2, 2) = "number 2b"
vArray(3, 2) = "number 3b"
Set objRegex = CreateObject("vbscript.regexp")
strIn = "a12stuff notme b34other missthis"
With objRegex
.Pattern = "([a-z]{1})(\d)(\d)([a-z]+)"
.Global = False
.IgnoreCase = True
.MultiLine = False
Do While .test(strIn)
Set objRegMC = .Execute(strIn)
For Each objRegM In objRegMC
Select Case objRegM.submatches(0)
Case "a"
strIn = .Replace(strIn, "" & vArray(1, 1) & vArray(2, 1) & vArray(3, 1))
Case "b"
strIn = .Replace(strIn, "" & vArray(1, 2) & vArray(2, 2) & vArray(3, 2))
End Select
Next
Loop
End With
MsgBox strIn
End Sub