2

I have some trouble with {}. When i get max value like this {1,8} it not work and i don't now why. Min vale is valid well

Private Sub Highlvl_Expression()
Dim strPattern As String: strPattern = "[a-zA-Z0-9_]{1,8}"
Dim strReplace As String: strReplace = ""
Dim regEx As New RegExp
Dim Test As Boolean
With regEx
     .Global = True
     .MultiLine = True
     .IgnoreCase = False
     .Pattern = strPattern
End With
Test = regEx.Test(Highlvl.Value)
If regEx.Test(Highlvl.Value) Then
    MsgBox ("Validate")
Else
    MsgBox ("Not Validate")
End If
End Sub
1
  • 1
    use anchors Dim strPattern As String: strPattern = "^[a-zA-Z0-9_]{1,8}$" Commented Aug 7, 2015 at 9:31

1 Answer 1

1

You specified the pattern that looks for 1 to 8 alphanumeric characters inside a string. If you run the regex against a 9-character string "ABCDE6789" (regEx.Execute("ABCDE6789")), you will have 2 matches: ABCDE678 and 9.

If you want to validate a string that should have a minimum or a maximum number of characters, you need to use anchors, i.e. start and end of string assertions ^ and $. So, use

Dim strPattern As String: strPattern = "^[a-zA-Z0-9_]{1,8}$"

And

.Global = False

The global flag is not necessary since we are not looking for multiple matches, but for a single true or false result with test.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.