Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterate through matches and replace for VBA RegEx

Tags:

regex

excel

vba

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

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.

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

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:

<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.

<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>
like image 562
Jon49 Avatar asked Oct 21 '22 21:10

Jon49


1 Answers

I think you could do something like this simplified example (given you input string was difficult to set-up to test)

  • Turn Global to False to use a single Regexp for each replacement (the alternative is to use Global = True but then run various Regexps for different first matches)
  • Use a Do loop to test whether a valid Regexp remains
  • Test the first submatch, and then use Select Case to run different routines to replace submatches $2-$4 (which I stored 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 = "([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
like image 62
brettdj Avatar answered Oct 27 '22 16:10

brettdj