Iterate through matches and replace for VBA RegEx

Is it possible to iterate through VBA RegEx matches and replace with the specific data given by the ID value?

eg.

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

with my template something like this:

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

So, I have several "replace" elements, each of which is unique to the UP:124 value.

Is this possible in VBA RegEx ? Just wanted to know before I go through a more cumbersome process! Thanks!

UPDATE (more information at the request of commentators - I hope this makes it clearer what I'm looking for!) I understand how to create templates that repeat the results more and then replace with each one, it detects that I am having problems . Thanks!):

This is the RegEx pattern I am using:

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

With settings like:

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

A sample replacement I would like to check is what the value of the first capture group is $1 , and then replace the {FILE} {PERCENT} {COLOR} values ​​(groups $2 , $3 and $4 ) with the corresponding values ​​that I saved in the class.

 <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
source share
1 answer

I think you could do something like this simplified example (given that the input line was difficult to test)

  • Turn Global to False to use one Regexp for each replacement (the alternative is to use Global = True , but then run different Regexps for different first matches)
  • Use the Do loop to check if Regexp remains valid
  • Check out the first submatch and then use Select Case to run the various routines to replace the submatches $2-$4 (which I saved in a simple array)

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 = "([az]{1})(\d)(\d)([az]+)" .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 
+3
source

All Articles