Reputation: 31
Need help building a VBScript regex that checks for a valid computer name and returns only invalid characters. String can contain numbers, upper and lower case letters, and (-) sign only. It can't start or end with (-) and cannot be only numbers.
Valid (Returns no match):
computer
Computer8
8Computer
Com8puter
Com-Puter
Computer-123
Invalid (Returns a match to invalid characters):
123
-computer
computer-
com*puter
PC&123
Upvotes: 1
Views: 10812
Reputation: 31
I ended up switching the valid and invalid returns. I also ended up using two different RegEx strings. The first is:
^[0-9a-zA-Z]{1,}[-]*[0-9a-zA-Z]{1,}$
This doesn't allow the (-) at the beginning or end and requires all numbers, letters, or (-). It also requires a string of at least two characters.
The second is:
"[a-zA-Z]"
This makes sure there is at least one letter included.
Upvotes: 2
Reputation: 155145
According to this: http://msdn.microsoft.com/en-us/library/ms974570.aspx VBScript has its own regex syntactic flavour. Note that NetBIOS computer names have a length limit of 15.
Then it should be "^\w[\w-]{0,14}$"
That RegEx satisfies all of the requirements except the "is all numbers". That can be done by running a second regex "^\d+$".
In code:
Dim regexValid, regexNumber
Set regexValid = New RegExp
Set regexNumber = New RegExp
regexValid.Global = True
regexValid.IgnoreCase = True
regexNumber.Global = True
regexNumber.IgnoreCase = True
regexValid.Pattern = "^\w[\w\-]{0,14}$"
regexNumber.Pattern = "^\d+$"
Dim inputString
inputString = InputBox("Computer name?")
If regexValid.Test( inputString ) And Not regexNumber.Test( inputString ) Then
' It's a valid computer name string
Else
' It's invalid
End If
Hmm, this is the first VBScript I've written this year.
Upvotes: 2
Reputation: 1759
Something like this /^([0-9]|[a-zA-Z]){1,}[a-zA-Z0-9-]+([0-9]|[a-zA-Z]){1,}$/
Upvotes: 1