smilykoch
smilykoch

Reputation: 57

Going from regex to word vba (.Find)

I have this regex

<#([^\s]+).*?>\s?<a href=""(.*?)"".*?>(.*?)</a>(\s?\((Pending|Prepared)\))?

And i really need it in a vba version for words .find method (don't need the matching-groups), here is what i have so far

\<\#*\>*\<a href=*\>*\<\/a\>

But i cant get the last part to work, here I'm talking about

(\s?\((Pending|Prepared)\))?

I really hope someone can help me, as regex in this case is not an option (Although i know i can use regex in VBA!)

Cheers

Upvotes: 2

Views: 363

Answers (1)

Blackhawk
Blackhawk

Reputation: 6140

I don't see an OR | in the documentation (Wildcard character reference) or the examples (Putting regular expressions to work in Word), so instead I suggest splitting it into two separate searches. The Word MVPs site has a good reference on the Word Regex as well if you want more information.

[^\s] can be written in the Word style regex as [! ] (note the space), + becomes @. It appears that neither the {n,} nor {n,m} syntax of VBA support an n value of 0, making ? and * hard to implement in Word. One option that the MS guys seem to use is *, which in Word is "Any string of characters". By my testing, * is lazy, meaning the pattern \<#*\> run against the string <#sometag> asdfsadfasdf > will only match <#sometag>. In addition, it can match 0 characters, for example \<\#*\> will match <#>.

So assuming that the first part is working as you expect, you could try the following two regex:

\<\#*\>*\<a href=*\>*\<\/a\>*\(Pending\)

and

\<\#*\>*\<a href=*\>*\<\/a\>*\(Prepared\)

The trouble here is that the * will match up until it hits the P of Pending or Prepared, so there could be other text in between, but it's the only way I can see of matching an optional space. If you can guaruntee that the space will or will not be there, that would go a long way towards making the regex safer.

Give that a try and see if it works for you!

Upvotes: 1

Related Questions