5

是否可以遍历VBA RegEx匹配项并替换为 ID 值给出的特定数据?

例如,

<a id="a-UP:124" {REPLACEITEMHERE}
.../a>

我的模式是这样的:

<a id="a-([\w\d]+:[\w\d]+)" ({REPLACEITEMHERE})

所以我有多个“替换项目”,每个项目的值都是唯一的UP:124

这可能VBA RegEx吗?只是想在我经历一个更繁琐的过程之前知道!谢谢!

更新(根据评论者的要求提供更多详细信息 - 希望这能让我更清楚我在寻找什么!我了解如何创建模式,它会更多地遍历结果,然后对我遇到的每个发现执行替换麻烦了。谢谢!):

这是我正在使用的 RegEx 模式:

<a id="a-([\w\d]+:[\w\d]+)"[^{]+({FILE})[^{]+({PERCENT})[^{]+({COLOR})

设置为:

.Global = True
.IgnoreCase = True
.MultiLine = False

我想要的替换模式是检查第一个捕获组的值是什么$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>
4

1 回答 1

3

我认为你可以做类似这个简化示例的事情(假设你输入的字符串很难设置来测试)

  • 转而Global为每个替换False使用一个Regexp(替代方法是使用Global = True但随后Regexps针对不同的第一次匹配运行各种)
  • 使用Do循环测试是否有有效的Regexp剩余物
  • 测试第一个submatch,然后使用Select Case运行不同的例程来替换submatches $2-$4(我存储在一个简单的数组中)

代码

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, "$1" & vArray(1, 1) & vArray(2, 1) & vArray(3, 1))
                Case "b"
                    strIn = .Replace(strIn, "$1" & vArray(1, 2) & vArray(2, 2) & vArray(3, 2))
                End Select
            Next
        Loop
    End With
    MsgBox strIn
End Sub
于 2012-12-23T02:42:07.223 回答