I'm trying to build a regex pattern that will return False
if a string starts with certain characters or contains non-word characters, but because VBA's RegExp
object doesn't support lookbehind, I am finding this difficult. The only word character prefixes that should fail are B_, B-, b_, b-.
This is my test code:
Sub testregex()
Dim re As New RegExp
re.pattern = "^[^Bb][^_-]\w+$"
Debug.Print re.Test("a24")
Debug.Print re.Test("a")
Debug.Print re.Test("B_")
Debug.Print re.Test(" a1")
End Sub
I want this to return:
True
True
False
False
but instead it returns
True
False
False
True
The problem is that the pattern looks for a character that's not in [Bb]
, followed by a character that's not in [-_]
, followed by a sequence of word characters, but what I want is just one or more word characters, such that if there are 2 or more characters then the first two are not [Bb][-_]
.