lundi 5 juin 2017

VBA Dynamic Regex Pattern Within "If"

I'm working on a tool to pull multiple forms of reference numbers from text and I'm struggling to think of a smart way to iterate through different RegEx patterns. Example of what I'm after is something like (pseudocode for what I'm after in curly braces):

Dim regEx as New RegExp
Dim someinput as String

regEx.Pattern = "201[67]\W?\d{6}"

If regEx.Test(someinput) = True Then
   foo
Elseif {with regEx.pattern="AB\W?\d{10}"  regEx.Test(someinput)} Then
   bar
...

This is going to be several levels deep as we've got around a dozen possible formats to check through. All I can think of at the moment is defining a new regEx item for each, setting the patterns for each, then running through the If->ElseIfs one by one, but there must be a better way... I hope!

Thanks much

Edit Just to pre-empt, foo and bar will be different depending on the input, so I can't just make (for instance) an array of possible patterns and run through checking each one and then returning a final "True/False"

Aucun commentaire:

Enregistrer un commentaire