1

I'm trying to use Outlook 2013 VBA to modify an email body by pulling out and replacing a < span> section. However, with multiple spans, I'm having trouble forcing the regex to only pick up one span.

Based on some other searches, I'm trying to use negative lookahead, but failing at it.

Result from below is: <span><span style = blah blah>Tags: test, test2</span>

Desired result is: <span style = blah blah>Tags: test, test2</span>

Code for test module:

Sub regextest()
Dim regex As New RegExp
Dim testStr As String

testStr = "a<span><span style=blah blah>Tags: test, test2</span></span>"

regex.pattern = "<span.*?(?:(span)).*?Tags:.*?</span>"

Set matches = regex.Execute(testStr)

For Each x In matches
    Debug.Print x 'Result: <span><span style = blah blah>Tags: test, test2</span>
Next
End Sub

Thank you!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Nathan Berton
  • 1,024
  • 8
  • 4

1 Answers1

1

Wiktor's answer in comments above works for my purposes:

<span\b[^<]*>[^<]*Tags:[^<]*</span>

This works as long as there are no '<' between the two span ends. Not really a lookahead, but it's good enough for what I'm doing and very simple.

Thanks Wiktor!

Nathan Berton
  • 1,024
  • 8
  • 4